This is a creation in Article, where the information may have evolved or changed.
1.struct Simplicity
This struct is similar to the C language, simulating the function of class, but not completely! No constructors and so on!
2.struct's statement
[PHP]
Package Main
Import "FMT"
Type person struct {
Age int
Name string
}
Func Main () {
Initialize two types of
A: = person{}
A.age = 2
A.name = "Widuu"
Fmt. Println (a)
B: = person{
Age:24,
Name: "Widuu",
}
Fmt. Println (b)
}
[/php]
3.go pointer operation
So we're going to change the value, take the memory address, and then change his reference on the memory address.
[PHP]
Package Main
Import "FMT"
Type person struct {
Age int
Name string
}
Func Main () {
B: = &person{
Age:24,
Name: "Widuu",
}
Fmt. Println (b)
G (b)
Fmt. Println (b)
}
Func G (per *person) {
Per. Age = 15
Fmt. Println (PER)
}
[/php]
4. Anonymous structure
(1) Use of anonymous internal structures
[PHP]
Func Main () {
A: = struct {
Name string
Age int
}{
Name: "Widuu",
Age:19,
}
Fmt. Println (a)
}
[/php]
(2) Anonymous internal structure use 2
[PHP]
Package Main
Import "FMT"
Type person struct {
Age int
Name string
Member struct {
Phone, city string
}
}
Func Main () {
A: = Person{age:16, Name: "Widuu"}
A.member.phone = "13800000"
a.member.city = "Widuuweb"
Fmt. Println (a)
}
[/php]
(3) Anonymous class values do not require a data name, two structures must be the same when assigning values
[PHP]
Package Main
Import "FMT"
Type person struct {
String
Int
}
Func Main () {
A: = person{"Joe", 19}
var b person
b = A
Fmt. Println (b)
}
[/php]
5. Embedding structure
1. Embedded structure Simulation Other programs have a concept of inheritance, just concept OH
[PHP]
Package Main
Import "FMT"
Type person struct {
Name string
Age int
}
Type student struct {
Person
Work string
}
Func Main () {
When instantiating an embedded structure without a data structure, the name defaults to the type name Person:person
A: = Student{person:person{name: "Widuu", age:19}, work: "IT"}
Fmt. Println (a)
}
[/php]
2. Structural methods
[PHP]
Package Main
Import "FMT"
Type A struct {
Name string//This is a common capitalization if the name is lowercase in the package can be used privately
}
Type B struct {
Name string
}
Func Main () {
A: = a{}
B: = b{}
A.print ()
B.print ()
}
The names of the same methods are taken by different type
Func (a *a) print () {
Fmt. Println ("A")
}
Func (b *b) print () {
Fmt. Println ("B")
}
[/php]
A little test, everybody explain why?
[PHP]
Package Main
Import "FMT"
Type Widuu int
Func Main () {
var W widuu
W.G (100)
Fmt. Println (W)
}
Func (a *widuu) G (num int) {
*a + = Widuu (num)
}
[/php]
without permission, may not reprint any article of this station: Micro network emoji language structure struct (universal Golang)