V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  gogogo1203  ›  全部回复第 3 页 / 共 28 页
回复总数  559
1  2  3  4  5  6  7  8  9  10 ... 28  
@pkoukk 因为一开始就吐槽错了,为了掩盖自己的错, 一直持续对线. 标题误导人, 他只是为了吐槽而已.
copilot x 有用过的吗? 好用么?
@tianxin8431 哈哈哈。 “只要我不同意的观点”===“学院派”。 业务上根本不难受,写一次就够。非要“公牛挤牛奶”,自己给自己找难受罢了。
@leoleoasd
json required tag 可以校验 not present
256 天前
回复了 zhuoyue100 创建的主题 Go 编程语言 怎么提升 golang 开发水平
@japeth 你看一下今天 go 区为了要给 unmarshl 吵了这么久, 就知道这个课程的价值。 不买也直接按照里面的 pattern 写 curd 。 各种舒服。
@leoleoasd 这有什么区别吗? struct 创建的时候就是默认零值,unmarshal 只是选择性宽容。你后续按自己业务需求去校验。一群人在喷一些什么东西。又不是所有场景都需要判断。
@BeautifulSoap 2023 年了,喜欢就用,不喜欢就换个语言,没必要抓个标准库输出。多一次运算累计起来的数量都是以亿计算的,我宁可相信 go team 是考虑过这些事的。
1 。老毕要是事先同意的话,没有必要搞得那么复杂,提前说话就行了。
2. 老毕要是被先斩后奏,不情不愿地被迫接受了这个“任务”。 第一回可能,第二回没有可能
是我迷糊了吗? 零值不是 创建 struct 的时候就有了吗??“The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil. Because null is often used in JSON to mean “not present,” unmarshaling a JSON null into any other Go type has no effect on the value and produces no error.” 自己读一读 doc 呗,unmarshaler 只是没有去改原先的零值而已。unmarshaler 要快,而不是提前做很多 validation.这些后续你可以慢慢搞。
@BeautifulSoap

```
// Create adds a Comment to the database. It returns the created Comment with
// fields like ID and DateCreated populated.
func (c Core) Create(ctx context.Context, nc NewComment, now time.Time, usrId string, tutId string) (Comment, error) {
if err := validate.Check(nc); err != nil {
return Comment{}, fmt.Errorf("validating data: %w", err)
}
parentId := &dbschema.NullString{}
if nc.ParentId == "" {
parentId.Valid = false
} else {
parentId.Valid = true
parentId.String = nc.ParentId
}
dbCm := db.Comment{
ID: validate.GenerateID(),
CommenterId: usrId,
TutorialId: tutId,
ParentId: parentId,
Body: nc.Body,
DateCreated: now,
DateUpdated: now,
}
if err := c.store.Create(ctx, dbCm); err != nil {
return Comment{}, fmt.Errorf("create: %w", err)
}

return toComment(dbCm), nil
}

```

web 到业务逻辑 然后再到 db 层,json tag 校验是必备的。sql.NullString sql.NullInt 都不是简单的 struct ,理论上不存在 go 后端不查 null 的,因为必须确认. 你们是不是用了什么 orm 一把梭了.
@delacey 我看过比较好的方式是 前端来的 json 用一个 struct, 校验完转成 db 专用的另一个 struct, db 查询返回的又用另外一个新的 struct. 前端更新某个 field , 又用一个专属的 struct. 用 go 写 curd 后台,做得最多的就是各个 struct 之间转来转去。
// Decode reads the body of an HTTP request looking for a JSON document. The
// body is decoded into the provided value.
//
// If the provided value is a struct then it is checked for validation tags.
func Decode(r *http.Request, val interface{}) error {
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(val); err != nil {
return err
}

return nil
}



type NewComment struct {
Body string `json:"body" validate:"required"`
ParentId string `json:"parentId"`
}




....
var nc comment.NewComment
if err := web.Decode(r, &nc); err != nil {
return fmt.Errorf("unable to decode payload: %w", err)
}

....

很久没有写 go 了,go 不至于连个 json 不能处理吧
256 天前
回复了 gogogo1203 创建的主题 程序员 [求推荐] chatgpt wrapper
@strawberrydafu 这个强, 这么多 stars 是真少见
我用了 3 年的 xmind ,已经一年多没打开了。
1. 思维导图节点一多,整个导图就失去作用了。节点太少也是没有作用. 思维导图就在两个状态里来回颠簸。
2. 单个文件的形式建立不起来个人知识库, 也不好搜索。
3. 大部分的笔记和信息都是搜集的时候爽,但是绝大部分都不会再次被打开。
苹果和 Unity 的 Vision Pro 合作是不是让 unity 飘了,想着提前占位,开创 per install 先河。
1  2  3  4  5  6  7  8  9  10 ... 28  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2309 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 26ms · UTC 05:43 · PVG 13:43 · LAX 22:43 · JFK 01:43
Developed with CodeLauncher
♥ Do have faith in what you're doing.