看板 Programming 關於我們 聯絡資訊
最近在摸索Go的相關語法碰到一些問題想請教一下 程式碼如下: type Person struct { Name string Age int } func test1(p *Person) { p.Name = "123" } func test(p any) { test1(p.(*Person)) ^^^^^^^^ } 想請問底線部分的*Person這個結構有什麼辦法從輸入p any動態產生嗎? 謝謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.218.181.99 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1662100823.A.CD1.html ※ 編輯: umaka0325 (180.218.181.99 臺灣), 09/02/2022 15:01:20
lycantrope: 可以用switch p.(type)分歧到不同case 133.51.216.29 09/02 16:20
lycantrope: 實際上any就是interface{} 133.51.216.29 09/02 16:22
umaka0325: 因為輸入p可能有很多種 用switch方式會 180.218.181.99 09/02 16:30
umaka0325: 需要寫很多case 所以才想用動態產生 180.218.181.99 09/02 16:30
lycantrope: 可能需要重構function吧. 133.51.216.29 09/02 16:42
lycantrope: p很多種但行為一樣就用interface定義 133.51.216.29 09/02 16:48