In other languages, this would be similar to the public access qualifier.
If the field (i.e. attribute) of the struct starts with an upper case, it would mean that that field is exported, thus accessible outside of the package.
Assume we have the following files in Go project:
1 2 3 |
|
We would define book.go in it's own package.
1 2 3 4 5 6 7 8 9 10 |
|
When using it in main.go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
In Ruby, this would be synonymous with using attr_accessor since we can:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
This is similar to private access qualifiers in other languages
If it starts with a lower case, the fields will not be accessible.
Try it for yourself!
Assuming your module name is myapp in go.mod
1 2 3 4 |
|
We create a new file in library/book.go under the package library
1 2 3 4 5 6 7 8 9 10 |
|
Import the package into main.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
If you have Go setup in VSCode, you would get the following lint error on the line:
1 |
|
The above is the detailed content of Golang Struct Field Scopes. For more information, please follow other related articles on the PHP Chinese website!