我刚开始学习GO。我正在尝试接收一个对象,但想通过指向底层对象的指针来更新它。但是代码不能正常工作,有人可以告诉我如何修复它吗?
package main
import (
"encoding/json"
"fmt"
)
type JSONStr struct {
str []byte
err error
}
type person struct {
First string
Last string
Age int
}
func test(ch chan *JSONStr) {
t := <-ch
p1 := person{
First: "James",
Last: "Bond",
Age: 32,
}
p2 := person{
First: "Miss",
Last: "Moneypenny",
Age: 27,
}
people := []person{p1, p2}
fmt.Println(people)
t.str, t.err = json.Marshal(people)
}
func main() {
ch := make(chan *JSONStr)
var result JSONStr
go test(ch)
ch <- &result
fmt.Println(result)
}
蓝山帝景
相关分类