package main
import "fmt"
type Person struct {
Age int
Name string
}
func main() {
//初始化兩種
a := Person{}
a.Age = 2
a.Name = "widuu"
fmt.Println(a)
b := Person{
Age: 24,
Name: "widuu",
}
fmt.Println(b)
}
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)
}
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)
}
package main
import "fmt"
type Person struct {
string
int
}
func main() {
a := Person{"joe", 19}
var b Person
b = a
fmt.Println(b)
}
package main
import "fmt"
type Person struct {
Name string
Age int
}
type student struct {
Person
work string
}
func main() {
//實(shí)例化時(shí) 如果嵌入式的結(jié)構(gòu)沒有數(shù)據(jù)結(jié)構(gòu)的名字 就默認(rèn)是類型名字Person:Person
a := student{Person: Person{Name: "widuu", Age: 19}, work: "IT"}
fmt.Println(a)
}
package main
import "fmt"
type A struct {
Name string //這個(gè)是共有的大寫 如果是小寫的name就包內(nèi)可以用私有的
}
type B struct {
Name string
}
func main() {
a := A{}
b := B{}
a.print()
b.print()
}
//通過type不同,來取相同的方法的名稱
func (a *A) print() {
fmt.Println("A")
}
func (b *B) print() {
fmt.Println("B")
}