V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
awanganddong

像这种结构怎么给赋值

  •  
  •   awanganddong · Mar 6, 2023 · 1822 views
    This topic created in 1156 days ago, the information mentioned may be changed or developed.
    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 replies    2023-03-06 14:01:27 +08:00
    rrfeng
        1
    rrfeng  
       Mar 6, 2023
    ToUser: struct {
    ID int `json:"id"`
    }{
    ID: 10,
    }
    ni9ht
        2
    ni9ht  
       Mar 6, 2023
    ```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  
       Mar 6, 2023 via Android
    goland 真的很好用
    awanganddong
        4
    awanganddong  
    OP
       Mar 6, 2023
    谢谢大家了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2807 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 13:55 · PVG 21:55 · LAX 06:55 · JFK 09:55
    ♥ Do have faith in what you're doing.