亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

更改函數內指針參數的值

更改函數內指針參數的值

Go
LEATH 2022-09-05 09:21:49
我被困在Go中似乎/應該很容易的事情上。我寫了一個小操場來更容易地解釋我的問題=>https://play.golang.org/p/Sm0SzrvEZS_o    package main    import (        "github.com/sirupsen/logrus"    )    type toto struct {        name string    }    func transform (data ...interface{}) {        logrus.Info("data before ", data)                data[0] = "tutu"                logrus.Info("data after ", data)    }    func main() {        var titi toto            logrus.Info("titi before ", titi) // -> empty            transform(&titi)            logrus.Info("titi after ", titi) // -> should have a name but don't    }目標是將結構傳遞給函數,在其中進行修改,并繼續在調用方函數中使用它??杀氖牵搮翟谧雍瘮抵羞M行了修改,但不會移動到調用方中。我是這門語言的初學者,也許我只是在某個地方錯過了一些東西......非常感謝您的幫助
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

這似乎做到了:


package main


type toto struct { name string }


func transform (data ...interface{}) {

   t := data[0].(*toto)

   t.name = "tutu"

}


func main() {

   var titi toto

   transform(&titi)

   println(titi.name == "tutu")

}


查看完整回答
反對 回復 2022-09-05
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

聽起來你想要一個指針。


在您的示例中,您使用了 一個數組,這有特殊原因嗎?一般來說,你應該在 Go 中顯式說明你的類型,特別是因為你正在處理一個簡單的結構。interface{}


package main


import (

    "log"

)


type toto struct {

    Name string

}


// to help with printing

func (t *toto) String() string {

    return t.Name

}


// transform takes an array of pointers to the toto struct

func transform(totos ...*toto) {

    log.Printf("totos before: %v", totos)


    // unsafe array access!

    totos[0].Name = "tutu"


    log.Printf("totos after: %v", totos)

}


func main() {

    // variables in Go are usually defined like this

    titi := toto{}


    transform(&titi)

}聽起來你想要一個指針。


在您的示例中,您使用了 一個數組,這有特殊原因嗎?一般來說,你應該在 Go 中顯式說明你的類型,特別是因為你正在處理一個簡單的結構。interface{}


package main


import (

    "log"

)


type toto struct {

    Name string

}


// to help with printing

func (t *toto) String() string {

    return t.Name

}


// transform takes an array of pointers to the toto struct

func transform(totos ...*toto) {

    log.Printf("totos before: %v", totos)


    // unsafe array access!

    totos[0].Name = "tutu"


    log.Printf("totos after: %v", totos)

}


func main() {

    // variables in Go are usually defined like this

    titi := toto{}


    transform(&titi)

}


查看完整回答
反對 回復 2022-09-05
?
MM們

TA貢獻1886條經驗 獲得超2個贊

在 Go 中,在函數中作為參數傳遞的變量實際上是變量的副本,而不是實際變量本身。如果要修改傳遞的變量,需要將其作為指針傳入。


就個人而言,每當我創建一個接受結構的函數時,我都會將該函數設置為接受指向該結構的實例的指針。這具有更高的內存效率的好處(因為我的程序不必在每次調用函數時都創建變量的副本),并且它允許我修改我傳遞的結構的實例。


這就是我的做法:


package main


import (

    "github.com/sirupsen/logrus"

)


type toto struct {

    name string

}


func transform (t *toto) {

    logrus.Info("t before: ", t)

    

    // since t is a pointer to a toto struct, 

    // I can directly assign "tutu" to the "name" field 

    // using the "dot" operator 

    t.name = "tutu"

    

    logrus.Info("t after: ", t)

}


func main() {

    // init a pointer to a toto instance

    titi := &toto{}

    

    logrus.Info("titi before: ", titi) // -> empty

    

    // this works because transform() accepts a pointer

    // to a toto struct and and titi is a pointer to a toto instance

    transform(titi)

    

    logrus.Info("titi after (as a pointer): ", titi) // -> not empty

    logrus.Info("titi after (as a value): ", *titi) // -> also not empty

}


查看完整回答
反對 回復 2022-09-05
  • 3 回答
  • 0 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號