go 打印结构体指针数组

 序列化后输出。

package main
 
import (
	"encoding/json"
	"fmt"
)
 
type Student struct {
	Age  int
	Name string
}

func printStructPointerSlice(data interface{}) string {
	bytes, err := json.Marshal(data)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return string(bytes)
}

func main() {
	s := []*Student{{Age: 17, Name: "abc"}, {Age: 18, Name: "efg"}}
	fmt.Printf("%v %+v\n", s, s)
	fmt.Printf("%s\n", printStructPointerSlice(s))
}

posted on 2024-12-30 09:25  王景迁  阅读(25)  评论(0)    收藏  举报

导航