- Security with Go
- John Daniel Leon
- 238字
- 2021-06-30 19:06:47
Struct
In Go, a struct or data structure is a collection of variables. The variables can be of different types. We will look at an example of creating a custom struct type.
Go uses case-based scoping to declare a variable either public or private. Variables and methods that are capitalized are exported and accessible from other packages. Lowercase values are private and only accessible within the same package.
The following example creates a simple struct named Person and one named Hacker. The Hacker type has a Person type embedded within it. An instance of each type is then created and the information about them is printed to standard output:
package main
import "fmt"
func main() {
// Define a Person type. Both fields public
type Person struct {
Name string
Age int
}
// Create a Person object and store the pointer to it
nanodano := &Person{Name: "NanoDano", Age: 99}
fmt.Println(nanodano)
// Structs can also be embedded within other structs.
// This replaces inheritance by simply storing the
// data type as another variable.
type Hacker struct {
Person Person
FavoriteLanguage string
}
fmt.Println(nanodano)
hacker := &Hacker{
Person: *nanodano,
FavoriteLanguage: "Go",
}
fmt.Println(hacker)
fmt.Println(hacker.Person.Name)
fmt.Println(hacker)
}
You can create private variables by starting their name with a lowercase letter. I use quotation marks because private variables work slightly different than in other languages. The privacy works at the package level and not at the class or type level.
- 數(shù)學(xué)也可以這樣學(xué):自然、空間和時(shí)間里的數(shù)學(xué)
- ANSYS 12.0有限元分析完全手冊(cè)
- 隨機(jī)數(shù)學(xué)及其應(yīng)用
- 數(shù)學(xué)原來可以這樣學(xué):初中篇
- 幾何公差那些事兒
- 博弈論與信息經(jīng)濟(jì)學(xué):PBL教程
- 數(shù)學(xué)女孩5:伽羅瓦理論
- 高等數(shù)學(xué)習(xí)題全解與學(xué)習(xí)指導(dǎo)(下冊(cè))
- 這才是好讀的數(shù)學(xué)史
- 說不盡的圓周率
- 微積分Ⅱ
- 高等數(shù)學(xué)習(xí)題全解(上冊(cè))
- 數(shù)學(xué)簡(jiǎn)史
- 復(fù)分析:可視化方法
- 數(shù)學(xué)分析新講(第3冊(cè))