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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

覆蓋全局變量

覆蓋全局變量

Go
largeQ 2023-07-26 17:40:56
我想知道為什么以下代碼中沒(méi)有警告或錯(cuò)誤允許我覆蓋全局變量。// You can edit this code!// Click here and start typing.package mainimport "fmt"type MyStruct struct {    MyInt uint32}func (s *MyStruct) MyMethod() {    fmt.Println(s.MyInt)}var theStruct MyStructfunc main() {    // Override the above global variable    // I would expect some kind of warning or error here?    theStruct := MyStruct {        MyInt: 42,    }    // Outputs 42    theStruct.MyMethod()    // Outputs 0    UseMyStruct()}func UseMyStruct() {    theStruct.MyMethod()}
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

變量可以隱藏父作用域中的其他變量。在您的示例中,范圍層次結(jié)構(gòu)如下所示:


global (scope)

├── theStruct (variable)

└── main (scope)

? ? └── theStruct (variable)

像這樣的陰影通常是通過(guò)以下方式完成的err:


package main


import (

? ? "io/ioutil"

? ? "log"

)


func main() {

? ? f, err := ioutil.TempFile("", "")

? ? if err != nil {

? ? ? ? log.Fatal(err)

? ? }

? ? defer f.Close()


? ? // This err shadows the one above, it is technically in its

? ? // own scope within the "if".

? ? if _, err := f.Write([]byte("hello world\n")); err != nil {

? ? ? ? log.Fatal(err)

? ? }


? ? if true {

? ? ? ? // We can even shadow with different types!

? ? ? ? err := 3


? ? ? ? log.Println(err)

? ? }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-26
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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