結構字段範圍
導出字段
在其他語言中,這類似於公共存取限定符。
- 如果你像我一樣來自 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中文網其他相關文章!

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

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 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版
中文版,非常好用