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

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

指針與值。

指針與值。

Go
蕭十郎 2023-05-08 16:36:55
我有點想知道為什么下面的代碼確實有效:var serverStartedTime time.Time // Holds the time since the server is started.type ServerInformation struct {    Uptime ServerUptimeInformation `json:"uptime"`}type ServerUptimeInformation struct {    Hours       int64 `json:"hours"`    Minutes     int64 `json:"minutes"`    Seconds     int64 `json:"seconds"`    NanoSeconds int64 `json:"nanoSeconds"`}func main() {    serverStartedTime = time.Now()    http.HandleFunc("/api/v1/health", getHealthHandler)    log.Fatal(http.ListenAndServe(":8000", nil))}func handler(writer http.ResponseWriter, request *http.Request) {    fmt.Fprintf(writer, "URL.Path = %q\n", request.URL.Path)}func getHealthHandler(writer http.ResponseWriter, request *http.Request) {    serverUptime := time.Now().Sub(serverStartedTime)    hours := int64(serverUptime.Hours())    minutes := int64(serverUptime.Minutes()) - (hours * 60)    seconds := int64(serverUptime.Seconds()) - (hours * 60) - (minutes * 60)    nanoSeconds := int64(serverUptime.Nanoseconds()) - (hours * 60) - (minutes * 60) - (seconds * 1000000000)    serverInformation := ServerInformation{        ServerUptimeInformation{            hours, minutes, seconds, nanoSeconds,        },    }    returnJSON(writer, serverInformation)}func returnJSON(writer http.ResponseWriter, data ...interface{}) {    dataJSON, marshalError := json.Marshal(data)    if marshalError != nil {        writer.WriteHeader(http.StatusInternalServerError)    } else {        writer.WriteHeader(http.StatusOK)        writer.Header().Set("Content-Type", "application/json")        writer.Write(dataJSON)    }}默認情況下,Go 復(fù)制提供給方法的參數(shù)。因此,“/api/v1/health”的 HTTP 處理程序確實需要一個編寫器,我們將其傳遞給該returnJSON方法。因此,此方法確實收到了它寫入的副本。我怎么會在我的瀏覽器中看到響應(yīng)?沒想到作者被抄襲了。
查看完整描述

1 回答

?
慕的地10843

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

你認為ResponseWriter是一個結(jié)構(gòu)體,但它是一個接口。

每次您發(fā)送writer http.ResponseWriter到您的方法時,您都會發(fā)送指向?qū)崿F(xiàn)該接口的結(jié)構(gòu)的指針。

執(zhí)行此行以查看實際類型:

fmt.Printf("%T\n",?writer)


查看完整回答
反對 回復(fù) 2023-05-08
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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