结构字段范围
导出字段
在其他语言中,这类似于公共访问限定符。
- 如果你像我一样来自 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 结构字段范围的详细内容。更多信息请关注PHP中文网其他相关文章!

Golang在实际应用中表现出色,以简洁、高效和并发性着称。 1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器