Go language structure
Structure is defined
type Point struct{
X int
Y float32
Z string
s []int
}
1.注意定义结构体时并没有分配内存空间
2.若结构体名称或者成员变量名称首字母为大写,代表这个字段的数据可以被其他包引用
小写为私有,只能在本包使用
Memory layout and allocation mechanisms
Note:
- Is only a definition of the structure describing the layout of the memory, only when the structure is instantiated, it will actually allocate memory. Examples of the memory region is created with a format consistent with the structure according to the format defined.
- After creating a structure variable, if there is no assignment to field, corresponds to a value of zero (default value), a boolean false, 0 is an integer, string is "", the default array type and its associated element type.
Zero value of the pointer, slice, map the is nil, i.e. not been allocated.
type Person struct {
Name string
Age int
Score [5]float32
ptr *int
slice []int
map1 map[string]string
}
func main(){
var p1 Person
fmt.Println(p1)
}
So if the premise pointer, slice, map the structure variables appear, when you create a structure variable assignments need to assign memory space make (), or will be error
p1.slice = make([]int,10)
p1.slice[0] = 666
p1.map1 = make(map[string]string)
p1.map1["key1"] = "value1"
var Ptr int
p1.ptr = new(int)
p1.ptr = &Ptr
- Is a value type structure, even if the assignment copy , different variables independent field structure, a field variable changing structure, the structure will not affect the other variable field
type Animal struct {
Name string
Age int
}
func main(){
var a1 Animal
var a2 Animal
a1.Name = "monkey"
a1.Age = 10
a2 = a1
a2.Name = "dog"
fmt.Println("a1:",a1)
fmt.Println("a2:",a2)
}
//注意a1和a2是两个独立的数据空间
Important chestnuts:
type Animal struct{
Name string
Age int
}
func main(){
var a1 Animal
a1.Name = "monkey"
a1.Age = 10
var a2 *Animal = &a1
fmt.Println((*a2).Age)
fmt.Println(a2.Age)
a2.Name = "lion"
fmt.Printf("a2.Name=%v a1.Name=%v \n",a2.Name,a1.Name)
fmt.Printf("(*a2).Name=%v a1.Name=%v \n",(*a2).Name,a1.Name)
fmt.Printf("a1的地址是:%p \n",&a1)
fmt.Printf("a2的地址是:%p a2的值是:%p \n",&a2,a2)
}
Special Note: can not write something like that, point .
number is greater than the priority of the operation asterisk *
, if the write will first use a2.Age, of course, the success of the visit, but then the error will appear asterisk *
var a2 *Animal = &a1
fmt.Printf(*a2.Age)
- All fields of the structure in memory is continuous, as in my system 64 inside an int number of bytes occupied by data of 8 bytes, the following results (in hexadecimal address), are separated by 8 bytes is continuously distributed
type Animal struct{
Name int
Age int
}
type Family struct {
leftA,rightA Animal
}
/*
//若是定义的指针类型,这两个指针类型的本身地址也是连续的
//但是它们指向的地址不一定是连续的
type Family2 struct{
leftA,rigthA *Point
}
*/
func main(){
a1 := Family{Animal{10,20},Animal{10,19}}
fmt.Printf("a1.leftA.Name 的地址是:%p \n", &a1.leftA.Name)
fmt.Printf("a1.leftA.Age 的地址是:%p \n",&a1.leftA.Age)
fmt.Printf("a1.rightA.Name 的地址是:%p \n",&a1.rightA.Name)
fmt.Printf("a1.rightA.Age的地址是:%p \n",&a1.rightA.Age)
}
Examples of structure
① Examples of the basic form
var ins T
1.T为结构类型,ins为结构体的实例
2.注意这里实例化一个ins,这个ins已经分配了内存空间
② to create a structure pointer type
ins := new(T)
T为类型,可以是结构体、整型、字符串等
ins:T类型被实例化后保存到ins变量中,ins的类型为*T,属于指针
③ address structure taken instantiation
Recommended var p Point = Point {}
ins := &T{}
T表示结构体类型
ins为结构体实例,类型为*T,为指针类型
Examples of the above three forms chestnuts
type Point struct{
X int
Y string
Val *int
}
//基本实例化
//p1 := Person{10,"monkey",&xxx}
var p1 Point
p1.X = 10
p1.Y = "monkey"
//创建指针类型的结构体
//var p2 Point = new(Point)
p2 := new(Point)
//(*p2).X = 6
p2.X = 6
p2.Y = "monkey"
//取结构体地址实例化
//var p3 *Point := &Point{6,"monkey",&xxx}
//(*p3).X = 6
var version = 1
p3 := &Point{}
p3.X = 6
p3.Y = "monkey"
p3.Val = &version
Examples of address - is the most widely used way of example structure, taken address on the package instantiates its initialization functions available
func newPoint(x int,y string,val *int)*Point{
return &Point{
X: x,
Y: y,
Val:val,
}
}
var version int
ins = newPoint(
8,
"monkey",
&version
)
Member variable initialization structure
Using the initialization key structure
type People struct{
name string
child *People
//relation由People类型取地址后,形成类型为*People的实例
relation := &People{
name:"爷爷"
child:&People{
name:"爸爸"
child:&People{
name:"我"
},
},
}
List initialization values using a plurality of structure
requires a certain sequence
type Address struct{
Provice string
City string
ZipCode int
PhoneNumber string
}
addr := Address{
"四川",
"成都",
610102,
"12",
}
fmt.Println(addr)//{四川 成都 610102 12}
Anonymous structure initialization
anonymous type structure no name, no keywords defined by type can be used directly
package main
import "fmt"
func printMsgType(msg *struct{
id int
data string}){
fmt.Println("%T\n",msg)
}
func main(){
msg := &struct{
id int
data string
}{
1024,
"monkey",
}
printMsgType(msg)
}
Notes structure
- Structure is a single user-defined type, requires identical fields (name, number, type), and other types of conversion when
type A struct{
Num int
}
type B struct{
Num int
}
func main(){
var a A
var b B
a = A(b)//可以转换
}
- Redefined body structure type (corresponding alias), golang considered a new data type, but can be converted to each other
type Point struct{
x int
y int
}
type Pot Point
type integer int
func main(){
var p1 Point
var p2 Pot
// p2 = p1//这是错误的
p2 = Pot(p1)
var num1 integer = 6
var num2 int = 8
//num2 = num1//这是错误的
num2 = int(num1)
}
- In front of the structure is defined is mentioned, "If the structure name or field name in uppercase, this field represents the data can be referenced in other packages, lowercase is private and can only be used in this package", here called package "encoding / json" approach json.Marshal (), passing parameters Animal structure of a variable, structure variable in the field name is capitalized, namely the public, that their method can call other packages, but if you change lowercase initials, in the output, the string is empty JSON
package main
import (
"encoding/json"
"fmt"
)
type Animal struct{
Name string `json:"name"`
Age int `json:"age"`
}
/*
type Animal struct{
name string
age int
*/
func main(){
monkey := Animal{"monkey",10}
jsonMon,err := json.Marshal(monkey)
if err!=nil{
fmt.Println("json处理错误",err)
}
fmt.Println(jsonMon)
fmt.Println(string(jsonMon))
}