Golang 结构字段范围
来源:dev.to
时间:2024-09-06 11:33:56 357浏览 收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Golang 结构字段范围》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
结构字段范围
导出字段
在其他语言中,这类似于公共访问限定符。
- 如果你像我一样来自 ruby,这将使用 attr_accessor 定义属性
如果结构体的字段(即属性)以大写开头,则意味着该字段已导出,因此可以在包外部访问。
假设go项目中有以下文件:
main.go /library /book.go
我们将在它自己的包中定义 book.go。
// library/book.go // assume we have a package called "library" which contains a book. package library // struct that represents a physical book in a library with exported fields type book struct { title string, author string }
在main.go中使用时:
package main import ( "fmt" "library" // importing the package that the struct book is in ) func main() { book := library.book{ title: "book title", author: "john snow" } // print the title and author to show that the struct book fields are accessible outisde it's package "library" fmt.println("title:", book.title) fmt.println("author:", book.author) }
在 ruby 中,这与使用 attr_accessor 是同义的,因为我们可以:
- 在类外读写属性值
class book # allow read and write on the attributes from outside the class attr_accessor(:title, :author) def initalize(title = nil, author = nil) @title = title @author = authoer end end # usage outside of the class book = book.new() # assinging attributes outside of the class book.title = "book title" book.title = "jon snow" # accessing attributes outside of the class puts book.title, book.author
私人领域
这类似于其他语言中的私有访问限定符
如果以小写开头,则这些字段将不可访问。
亲自尝试一下!
假设你的模块名称是 go.mod 中的 myapp
// go.mod module myapp go 1.22.5
我们在包library下的library/book.go中创建一个新文件
// library/book.go // assume we have a package called "library" which contains a book. package library // fields start with lowercase, fields are not exported type book struct { title string author string }
将包导入main.go
// main.go package main import ( "fmt" // import the library package "myapp/library" ) func main() { book := library.book{ title: "book title", author: "john snow" } // print the title and author to show that the struct book fields are accessible outisde it's package "library" fmt.println("title:", book.title) fmt.println("author:", book.author) }
如果您在 vscode 中设置了 go,您会收到以下 lint 错误:
- 标题:“书名
unknown field author in struct literal of type library.Bookcompiler[MissingLitField](https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#MissingLitField
好了,本文到此结束,带大家了解了《Golang 结构字段范围》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
505 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
486 收藏
-
276 收藏
-
419 收藏
-
201 收藏
-
432 收藏
-
208 收藏
-
102 收藏
-
362 收藏
-
271 收藏
-
184 收藏
-
364 收藏
-
312 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习