Is it possible to define an immutable struct in Golang? Once initialized then only read operation on struct's field, no modification of field values. If so, how to do that.
关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率

首页/
已采纳
Golang中的不可变结构
收起
- 写回答
- 好问题 0 提建议
- 关注问题
微信扫一扫
点击复制链接分享
- 邀请回答
- 编辑 收藏 删除 结题
- 收藏 举报
2条回答 默认 最新
- 关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率
dqzuo0327 2017-12-04 04:03关注It is possible to make a struct to be read-only outside of package by making it's members non-exported and providing readers. For example:
package mypackage type myReadOnly struct { value int } func (s myReadOnly) Value() int { return s.value } func NewMyReadonly(value int) myReadOnly{ return myReadOnly{value: value} }
And usage:
myReadonly := mypackage.NewMyReaonly(3) fmt.Println(myReadonly.Value()) // Prints 3
本回答被题主选为最佳回答 , 对您是否有帮助呢? 本回答被专家选为最佳回答 , 对您是否有帮助呢? 本回答被题主和专家选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏举报微信扫一扫
点击复制链接分享
编辑预览轻敲空格完成输入- 显示为
- 卡片
- 标题
- 链接
评论按下Enter换行,Ctrl+Enter发表内容
编辑
预览

轻敲空格完成输入
- 显示为
- 卡片
- 标题
- 链接
报告相同问题?
提交
- 2015-08-10 23:16回答 1 已采纳 You can use an xml.Name field to specify the tag name you want it in the XML output. Note that wit
- 2019-05-28 23:29回答 2 已采纳 Unfortunately or not, there is no way to do this in Go. First tool that comes to mind is reflectio
- 2018-02-21 08:53回答 4 已采纳 You should use RawMessage in the type definition type GeneralResponse struct { Success string
- 2021-05-26 10:37用于Go的不可变数据结构基于不可变的持久性哈希数组映射树(HAMT),与核心HAMT实现的最小接口,因此您可以轻松地在其之上实现自己的结构。 附带了一些准备就绪的set和map实现,受Clojure的数据结构的启发。出色的...
- 2019-09-04 07:47回答 1 已采纳 Those are not embedded; embedding has a specific meaning in Go, as outlined in the spec, and as ex
- 2017-05-02 20:30回答 1 已采纳 error in not a struct, is an interface. type error interface { Error() string } you can de
- 2017-04-29 13:20回答 1 已采纳 You can construct your one statement by enumerating all the parameters with their placeholders and
- 2020-09-19 17:53在Golang中,自定义函数是程序设计的基础组成部分,它们帮助组织代码,实现特定功能,并且可以在程序的各个部分重复使用。以下是对Golang自定义函数的详细解释: 1. **函数声明**: - 使用`func`关键字开始函数...
- 2018-11-29 03:34回答 4 已采纳 You're unmarshaling an array to a map. That obviously won't work. you need val to be an array. fu
- 2015-10-12 18:08回答 2 已采纳 You're mis-using the variadic shorthand in your call to fmt.Println(). What you're actually sendi
- 2019-03-02 18:14回答 2 已采纳 Your logic assumes that the length of a is always the proper count to conditionally iterate throug
- 2021-05-26 08:38Go中的不可变和高效内存映射和集。 该程序包hamt提供了映射(关联数组)和实现为哈希数组映射尝试(HAMT)的集合的不可变集合类型。 Go中不可变和高效存储映射和集合的所有操作。 该程序包hamt提供了映射(关联数组...
- 2020-09-16 00:19总之,Golang不支持三元运算符是出于对代码简洁性和可读性的重视。虽然这可能意味着在编写条件赋值时需要更多的代码行,但它鼓励开发者写出更清晰、更易于理解的代码,这对长期的项目维护和团队协作是非常有益的。在...
- 2025-02-10 14:55alden_ygq的博客 以上目录结构就是我Golang Web 单体项目的最佳实践,能够提升代码的可维护性和可扩展性。随着业务的发展,该结构可以很容易地拆分成微服务架构。希望这篇文章能帮助大家在实际项目中更好地组织代码,提升 Golang ...
- 2024-06-17 02:23最后,**错误处理与日志记录**:为了保证工具的健壮性,错误处理是必不可少的。当遇到无法解决的差异或执行SQL脚本时出现错误,工具应有适当的方式来记录这些事件,并通知用户。日志记录可以帮助开发者追踪问题,...
- 没有解决我的问题, 去提问
联系我们(工作时间:8:30-22:00)
400-660-0108kefu@csdn.net在线客服
- 京ICP备19004658号
- 经营性网站备案信息
公安备案号11010502030143
- 营业执照
- 北京互联网违法和不良信息举报中心
- 家长监护
- 中国互联网举报中心
- 网络110报警服务
- Chrome商店下载
- 账号管理规范
- 版权与免责声明
- 版权申诉
- 出版物许可证
- ©1999-2025北京创新乐知网络技术有限公司