请教下 go 怎么定义范型的

2023-01-11 19:49:05 +08:00
 awanganddong

json 返回的值如下

有值的情况下
        "nobility": {
                   "name": "",
                   "image": "",
                   "desc": ""
               }
无值的情况下是 

"nobility": {}

我该怎么定义
1844 次点击
所在节点    Go 编程语言
22 条回复
MidGap
2023-01-11 19:54:01 +08:00
这个和泛型看起来没什么关系。struct 就是
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
2023-01-11 20:00:39 +08:00
https://pastebin.com/6mC3Pn6W 根据你的需求情况选择一种就可以了
awanganddong
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
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
2023-01-11 20:10:24 +08:00
我代码是这样的
polythene
2023-01-11 21:19:17 +08:00
json tag 里面加个 omitempty
awanganddong
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
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
2023-01-11 21:30:15 +08:00
@MidGap 我按你的方式写,我不知道怎么给 Nobility 这个 struct 赋值了。

类似于 4 楼的问题
awanganddong
2023-01-11 21:44:16 +08:00
@MidGap 我明白你的意思了,谢谢大家了。
awanganddong
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
2023-01-12 09:45:06 +08:00
为啥不把 ListenAndServe 放在外面,Shutdown 放 go func 里面
awanganddong
2023-01-12 09:50:37 +08:00
@yaott2020 这块是 gin 的官方 demo ,我直接搬过来了
yaott2020
2023-01-12 09:59:32 +08:00
5s 会不会太长了
awanganddong
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
2023-01-12 10:41:44 +08:00
找到个好东西

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

https://cloud.tencent.com/developer/ask/sof/76384
MidGap
2023-01-12 22:17:31 +08:00
端口被占用的话 1.看看是不是别的进程占用了 2. 思考一下进程结束了端口真的被释放了吗?
MidGap
2023-01-12 22:20:17 +08:00
@MidGap 抱歉 没看清除是 supervisor 维护的。supervisor 重启应该要杀 pid 吧?不是太了解这个。可能没有真的结束进程

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/908242

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX