V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
awanganddong
V2EX  ›  Go 编程语言

像这种结构怎么给赋值

  •  
  •   awanganddong · 2023-03-06 12:10:42 +08:00 · 1254 次点击
    这是一个创建于 428 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type List struct {
    	Content string `json:"content,omitempty"`
    	ToUser  struct {
    		ID int `json:"id"`
    	} `json:"to_user,omitempty"`
    }
    
    func main() {
    	var list []List
    	list = append(list, List{
    		Content: "hello world",
    	}, List{
    		ToUser: ,  //在这个环节赋值,我不知道怎么处理了
    	})
    	res, err := json.Marshal(list)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(string(res))
    }
    
    4 条回复    2023-03-06 14:01:27 +08:00
    rrfeng
        1
    rrfeng  
       2023-03-06 12:16:57 +08:00
    ToUser: struct {
    ID int `json:"id"`
    }{
    ID: 10,
    }
    ni9ht
        2
    ni9ht  
       2023-03-06 12:21:41 +08:00
    ```go
    // 1
    list = append(list, List{
    Content: "hello world",
    }, List{
    ToUser: struct {
    ID int `json:"id"`
    }{
    ID: 1,
    },
    })

    // 2
    item := List{
    Content: "hello world",
    }
    item.ToUser.ID = 1
    list = append(list, List{
    Content: "hello world",
    }, item)
    ```
    yaott2020
        3
    yaott2020  
       2023-03-06 12:47:57 +08:00 via Android
    goland 真的很好用
    awanganddong
        4
    awanganddong  
    OP
       2023-03-06 14:01:27 +08:00
    谢谢大家了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3900 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:17 · PVG 12:17 · LAX 21:17 · JFK 00:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.