2024年最新Go中结构体字段标签(Struct Tags)详解_go struct tag(1),【深夜思考

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

1. JSON序列化和反序列化

这可能是字段标签最常见的应用。encoding/json库会查找结构体中每个字段的json标签来确定如何将该字段映射到JSON对象。

p := Person{Name: "John", Age: 30}
jsonStr, _ := json.Marshal(p)
// jsonStr: {"name":"John","age":30}

2. 数据库ORM映射

许多Go的数据库ORM库(如gorm)也使用字段标签来控制数据库列名。

type User struct {
    ID   int `gorm:"column:User_ID"`
    Name string `gorm:"column:User_Name"`
}

3. 验证

字段标签还可以用于各种验证场景。例如,使用go-validator库进行字段验证:

type SignupRequest struct {
    Email    string `validate:"required,email"`
    Password string `validate:"required,gte=8"`
}

自定义标签

除了现有库对标签的预定义用法,我们还可以自定义标签以实现特定功能。

type MyStruct struct {
    Field string `myTag:"Important"`
}

// 使用反射读取自定义标签
t := reflect.TypeOf(MyStruct{})
field, _ := t.FieldByName("Field")
tag := field.Tag.Get("myTag")

总结

结构体字段标签在Go开发中是一个非常强大和灵活的工具,它让我们能够在保持代码简洁的同时,实现高度定制化的功能。

通过掌握字段标签,我们不仅能编写更加灵活和可维护的代码,而且也是向软件架构师和系统架构师迈进的一大步。

如果您觉得本文有用,或有任何疑问和建议,欢迎在下面留言。同时,如果你对企业管理,团队管理,项目管理或个人成长有兴趣,也请关注我们的其他文章。

祝编程愉快!

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

/***************************************************************************** Exports the specified objects to the specified part. The objects are copied into the destination part. Any other transferable objects upon which the given objects depend are also exported. Note that the export operation fails if there is no display part. NOTE: Object occurrences and part occurrences are not exportable. If an object depends on another object which is not exportable, then that object is not exported and a warning value of UF_PART_warn_objects_not_copied is returned. However, the operation continues, and other requested exportable objects are still exported. This situation can arise in two circumstances. 1. A drafting object is not exported if its associated geometry is not exported. 2. Datums cannot be exported if feature parameters are removed. If any other error occurs, the operation does not succeed and the appropriate error code is returned. Environment: Internal and External See Also: UF_PART_export History: For V14.0, the behavior of this function was modified as follows: If you are exporting geometry to a part which is loaded (or partially loaded) in a session, then that part is no longer saved and closed as a consequence of calling this routine. The destination part is only saved and closed as part of this operation if it was not loaded beforehand. This makes the Open API behavior consistent with the interactive behavior. For V14.0.1, the behavior of this function was modified as follows: When exporting geometry using this function, the geometry would not be oriented properly in the destination part if the WCS of the source part was away from the origin. This is now fixed so that the geometry is transformed from the WCS of the source part to the WCS of the destination part. The Open API behavior is now the same as the interactive behavior. For V15.0, this function was modified as follows: The UF_PART_maintain_all_params enumerated constant was added to UF_PART_export_params_mode_t. ****************************************************************************/ extern UFUNEXPORT int UF_PART_export_with_options( const char * part_name ,/* <I> Name of part to export objects to. */ int num_objects ,/* <I> Number of objects in object_array to be exported. */ tag_t object_array[] ,/* <I,len:num_objects> Objects to be exported. */ UF_PART_export_options_p_t options /* <I> Pointer to struct containing options to control how the objects are exported. */ ); 说一说这个函数怎么用
最新发布
03-24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值