gpt4 book ai didi

go - 结构的一个字段,由 3 个项目组成。这怎么编译?

转载 作者:数据小太阳 更新时间:2023-10-29 03:40:39 25 4
gpt4 key购买 nike

我见过这样的代码:

type Product struct {
Name string `db:"product_name"`
Id int `db:"id"`
}

type Stocks {
Name string `db:"stock_name"`
Price float `db:"price"`
Type string `db:"type"`
}

虽然这段代码可以编译,但怎么可能呢?结构的成员必须由名称和以下类型组成。但是,这里还有 db:"product_name" 这是第三个成员

这怎么可能有效?从 Go 的角度来看,db:"product_name" 是什么?

最佳答案

The Go Programming Language Specification

Struct types

A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

关于go - 结构的一个字段,由 3 个项目组成。这怎么编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53819537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com