第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在同一文件中運行兩個匿名結(jié)構(gòu)?

如何在同一文件中運行兩個匿名結(jié)構(gòu)?

Go
繁華開滿天機 2022-09-19 21:12:40
在此代碼中,我有兩條消息。1-第一個是注冊您的帳戶已創(chuàng)建。現(xiàn)在登錄到一個帳戶,然后2-第二個是登錄失敗,即如果電子郵件或密碼不匹配,則給出錯誤消息。我已經(jīng)在文件中調(diào)用了這兩條消息,但是當我運行此代碼時,它更喜歡只運行第一條消息。login.html <div class="loginfailure">        <h1>{{.Loginfailure}}</h1>    </div>它不允許第二個消息,如果調(diào)用第二條消息,它將提供空白頁。處理程序.go注冊成功success := struct{ Signupsuccess string }{Signupsuccess: "Your account is successfully created"}loginTmpl.Execute(w, success)登錄失敗failure := struct{ Loginfailure string }{Loginfailure: "Enter the correct email or password"}loginTmpl.Execute(w, failure)登錄.html{{define "body"}}    <div class="loginfailure">        <h1>{{.Loginfailure}}</h1>    </div>    <div class="signupsuccess">        <h1>{{.Signupsuccess}}</h1>    </div>    <h1>Log In</h1>      <p>Login to access your account</p>    <form action="/login" method="POST">        <div>            <label for="email">Email</label>            <input type="email" name="email" placeholder="Enter your email address" required>        </div>        <div>            <label for="password">Password</label>            <input type="password" name="password" placeholder="Enter your password" required>        </div>        <div>            <input type="submit" value="Login">        </div>        <div>            <a href="/signup" class="link">Signup</a>        </div>    </form>{{end}}
查看完整描述

1 回答

?
喵喵時光機

TA貢獻1846條經(jīng)驗 獲得超7個贊

始終檢查 返回的錯誤。Execute


在模板內(nèi)部,您不能引用在傳遞給模板的結(jié)構(gòu)中不存在的字段,即當您傳遞操作時會破壞模板,當您傳遞中斷模板時。failure{{.Signupsuccess}}success{{.Loginfailure}}


您可以使用地圖,允許引用地圖中不存在的地圖鍵


success := map[string]string{"Signupsuccess": "Your account is successfully created"}

if err := oginTmpl.Execute(w, success); err != nil {

    panic(err)

}

failure := map[string]string{"Loginfailure": "Enter the correct email or password"}

if err := loginTmpl.Execute(w, failure); err != nil {

    panic(err)

}

或者使用具有兩個字段的單個結(jié)構(gòu)


type TemplateData struct {

    Signupsuccess string

    Loginfailure string

}

success := TemplateData{Signupsuccess: "Your account is successfully created"}

if err := oginTmpl.Execute(w, success); err != nil {

    panic(err)

}

failure := TemplateData{Loginfailure: "Enter the correct email or password"}

if err := loginTmpl.Execute(w, failure); err != nil {

    panic(err)

}


查看完整回答
反對 回復 2022-09-19
  • 1 回答
  • 0 關(guān)注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號