課程
/后端開發(fā)
/Go
/Go語言第一課
person.Address,address = address,person.Address
Move方法體里這么寫的同學(xué),麻煩解釋一下,謝謝。
2019-11-02
源自:Go語言第一課 6-2
正在回答
這個(gè)算是一個(gè)go的語法糖吧,可以一起賦值。
如 a,b = 1,2? ? 從左到右? a = 1, b = 2
當(dāng)右邊為變量時(shí),執(zhí)行賦值動(dòng)作是它此刻的值。
接上面例子? ? ?a,b = b,a。? 執(zhí)行此語句時(shí),a = 1,b =2? 所以相當(dāng)于 a,b = 2,1
這個(gè)語法糖在變量互換的時(shí)候異常好用
之前:
????a=1,b=2;
????temp = a;
? ? a = b;
????b = temp;
現(xiàn):
????a,b = b,a
Person結(jié)構(gòu)體里面有舊地址的內(nèi)容 北京? 所以oldAddress聲明并賦值person.Address
newAddress為新地址內(nèi)容 san Francisco 賦值給person.Address 因?yàn)槭侵羔標(biāo)詴?huì)修改Person本身
package?main import?"fmt" type?Person?struct?{ ????Name????string ????Gender??string ????Age?????uint8 ????Address?string??//?結(jié)構(gòu)體里加個(gè)屬性 } //?這里加個(gè)Move方法 func?(person?*Person)?Move(newAddress?string)?string{ ????oldAddress:=?person.Address ????person.Address?=?newAddress ????return?oldAddress } func?main()?{ ????p?:=?Person{"Robert",?"Male",?33,?"Beijing"} ????oldAddress?:=?p.Move("San?Francisco") ????fmt.Printf("%s?moved?from?%s?to?%s.\n",?p.Name,?oldAddress,?p.Address) }
舉報(bào)
Go語言入門教程,編程之必備知識擴(kuò)散,打好Go語言編程基礎(chǔ)
2 回答解釋一下整段代碼的含義
3 回答誰能給解釋一下為什么輸出結(jié)果是這樣
1 回答匿名變量能解釋一下嘛?
1 回答有人解釋下調(diào)用流程嗎?
2 回答chan的長度如何解釋
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2019-12-26
這個(gè)算是一個(gè)go的語法糖吧,可以一起賦值。
如 a,b = 1,2? ? 從左到右? a = 1, b = 2
當(dāng)右邊為變量時(shí),執(zhí)行賦值動(dòng)作是它此刻的值。
接上面例子? ? ?a,b = b,a。? 執(zhí)行此語句時(shí),a = 1,b =2? 所以相當(dāng)于 a,b = 2,1
這個(gè)語法糖在變量互換的時(shí)候異常好用
之前:
????a=1,b=2;
????temp = a;
? ? a = b;
????b = temp;
現(xiàn):
????a,b = b,a
2019-12-06
Person結(jié)構(gòu)體里面有舊地址的內(nèi)容 北京? 所以oldAddress聲明并賦值person.Address
newAddress為新地址內(nèi)容 san Francisco 賦值給person.Address 因?yàn)槭侵羔標(biāo)詴?huì)修改Person本身
2019-11-05