(编辑:jimmy 日期: 2024/12/24 浏览:2)
golang修改结构体中的切片值,直接传结构体地址就可以
package main import "fmt" type rspInfo struct { KeyWords string `json:"key_words"` Value []string `json:"value"` } func setSlice(te *[]string){ str := "12" *te = append(*te,str) } //结构提传指针 func setStruc(rsp *rspInfo){ str := "12" te := make([]string,0) te = append(te,str) te = append(te,str) rsp.KeyWords = "12" rsp.Value = te } func main() { //test := make([]string,0) //setSlice(&test) var rsp rspInfo setStruc(&rsp) fmt.Println(rsp) }