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

请教下 go 怎么定义范型的

  •  
  •   awanganddong · 2023-01-11 19:49:05 +08:00 · 1833 次点击
    这是一个创建于 464 天前的主题,其中的信息可能已经有所发展或是发生改变。

    json 返回的值如下

    有值的情况下
            "nobility": {
                       "name": "",
                       "image": "",
                       "desc": ""
                   }
    无值的情况下是 
    
    "nobility": {}
    
    我该怎么定义
    
    22 条回复    2023-01-13 10:21:18 +08:00
    MidGap
        1
    MidGap  
       2023-01-11 19:54:01 +08:00
    这个和泛型看起来没什么关系。struct 就是
    MidGap
        2
    MidGap  
       2023-01-11 19:54:38 +08:00
    @MidGap type T struct {
    Nobility struct {
    Name string `json:"name"`
    Image string `json:"image"`
    Desc string `json:"desc"`
    } `json:"nobility"`
    }


    不下发等于赋默认值
    janxin
        3
    janxin  
       2023-01-11 20:00:39 +08:00
    https://pastebin.com/6mC3Pn6W 根据你的需求情况选择一种就可以了
    awanganddong
        4
    awanganddong  
    OP
       2023-01-11 20:02:10 +08:00
    @MidGap
    ```
    nobilityInfo T.Nobility
    money := getMoney(val.Id, val.Gender)
    for _, value := range nobility {
    if money >= value.StartCoin && money <= value.EndCoin {
    nobilityInfo = T.Nobility{
    value.Name,
    pkg.AppSetting.QiniuUrl + value.Image,
    value.Desc,
    }
    break
    }
    }
    if val.Gender == 2 && val.NobilityClose == 2 {
    nobilityInfo = struct {}{}
    }
    ```
    类似于这种我该怎么赋值
    上边报错了
    awanganddong
        5
    awanganddong  
    OP
       2023-01-11 20:10:15 +08:00
    ```
    type Im struct{

    Nobility T `json:"nobility"`
    Time int `json:"time"`
    IsGoddess bool `json:"is_goddess"`
    }
    type T struct {
    Nobility struct {
    Name string `json:"name"`
    Image string `json:"image"`
    Desc string `json:"desc"`
    } `json:"nobility"`
    }
    ```
    awanganddong
        6
    awanganddong  
    OP
       2023-01-11 20:10:24 +08:00
    我代码是这样的
    polythene
        7
    polythene  
       2023-01-11 21:19:17 +08:00
    json tag 里面加个 omitempty
    awanganddong
        8
    awanganddong  
    OP
       2023-01-11 21:22:37 +08:00
    @polythene 比如我返回的值是下边的 json

    ```
    {
    "code": 200,
    "data": [
    {
    "id": 1,
    "nobility": {
    "name": "",
    "image": "",
    "desc": ""
    },
    "time": 0,
    "is_goddess": false
    },

    ],
    "msg": "ok"
    }
    ```
    我想要的效果是如果 nobility 任何一项为空,则就是空对象

    ```
    {
    "code": 200,
    "data": [
    {
    "id": 1,
    "nobility": {}, //上边的转化成这样
    "time": 0,
    "is_goddess": false
    },

    ],
    "msg": "ok"
    }
    ```
    MidGap
        9
    MidGap  
       2023-01-11 21:27:17 +08:00
    @awanganddong type T struct {
    Nobility struct {
    Name string `json:"name,omitempty"`
    Image string `json:"image,omitempty"`
    Desc string `json:"desc,omitempty"`
    } `json:"nobility,omitempty"`
    }


    加 ,omitempty ,这样你把这个结构转 JSON ,默认值就会不下发,比如 nil 0 "" false
    awanganddong
        10
    awanganddong  
    OP
       2023-01-11 21:30:15 +08:00
    @MidGap 我按你的方式写,我不知道怎么给 Nobility 这个 struct 赋值了。

    类似于 4 楼的问题
    awanganddong
        11
    awanganddong  
    OP
       2023-01-11 21:44:16 +08:00
    @MidGap 我明白你的意思了,谢谢大家了。
    awanganddong
        12
    awanganddong  
    OP
       2023-01-11 22:42:46 +08:00
    @MidGap 再请教个问题,
    我是通过 supervisor 来守护 go
    我通过 supervisorctl stop program 关闭程序
    然后再重启的时候,报接口被占用的错误
    2023/01/11 22:38:38 listen: listen tcp :8000: bind: address already in use

    我已经配置了优化退出,还是出问题。

    package main

    import (
    "context"
    "log"
    "net/http"
    "os"
    "os/signal"
    "time"

    "github.com/gin-gonic/gin"
    )

    func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
    time.Sleep(5 * time.Second)
    c.String( http.StatusOK, "Welcome Gin Server")
    })

    srv := &http.Server{
    Addr: ":8080",
    Handler: router,
    }

    go func() {
    // 服务连接
    if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
    log.Fatalf("listen: %s\n", err)
    }
    }()

    // 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
    quit := make(chan os.Signal)
    signal.Notify(quit, os.Interrupt)
    <-quit
    log.Println("Shutdown Server ...")

    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    if err := srv.Shutdown(ctx); err != nil {
    log.Fatal("Server Shutdown:", err)
    }
    log.Println("Server exiting")
    }
    yaott2020
        13
    yaott2020  
       2023-01-12 09:45:06 +08:00 via Android
    为啥不把 ListenAndServe 放在外面,Shutdown 放 go func 里面
    awanganddong
        14
    awanganddong  
    OP
       2023-01-12 09:50:37 +08:00
    @yaott2020 这块是 gin 的官方 demo ,我直接搬过来了
    yaott2020
        15
    yaott2020  
       2023-01-12 09:59:32 +08:00 via Android
    5s 会不会太长了
    awanganddong
        16
    awanganddong  
    OP
       2023-01-12 10:15:59 +08:00
    我设置成 1s,随后启动了 go 服务,然后通过 lsof -i:8000 查看 pid
    执行 kill -Hup pid
    这时候程序退出了,但是 8000 端口依然被监听。
    代码中监听了一下信号
    signal.Notify(quit, os.Interrupt, syscall.SIGQUIT, syscall.SIGHUP)
    awanganddong
        17
    awanganddong  
    OP
       2023-01-12 10:41:44 +08:00
    找到个好东西

    json to go
    https://mholt.github.io/json-to-go/
    awanganddong
        18
    awanganddong  
    OP
       2023-01-12 11:37:17 +08:00
    从结构中删除字段或将其隐藏在 JSON 响应中
    这个文章比较好

    https://cloud.tencent.com/developer/ask/sof/76384
    MidGap
        19
    MidGap  
       2023-01-12 22:17:31 +08:00
    端口被占用的话 1.看看是不是别的进程占用了 2. 思考一下进程结束了端口真的被释放了吗?
    MidGap
        20
    MidGap  
       2023-01-12 22:20:17 +08:00
    @MidGap 抱歉 没看清除是 supervisor 维护的。supervisor 重启应该要杀 pid 吧?不是太了解这个。可能没有真的结束进程
    awanganddong
        21
    awanganddong  
    OP
       2023-01-13 09:55:20 +08:00
    现在发现每次启动 supervisor 启动了两个进程。supervisor 只杀了其中一个 go 进程。还有一个没有关闭。所以非常奇怪
    awanganddong
        22
    awanganddong  
    OP
       2023-01-13 10:21:18 +08:00
    找到问题了,supervisorctl 配置 killasgroup=true ;默认为 false ,向进程组发送 kill 信号,包括子进程
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2579 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:29 · PVG 12:29 · LAX 21:29 · JFK 00:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.