type myInt int
type TypeAlias = Type
package main
import "fmt"
type newInt int //类型定义
type myInt = int //类型别名
func main() {
var a newInt
var b myInt
fmt.Printf("type of a:%T\n",a) //type of a:main.newInt
fmt.printf("type of b:%T\n",b) //type of b:int
}
package main
import "fmt"
type person struct {
name string
city string
age int
}
func main() {
var p1 person
p1.name = "Snail"
p1.city = "上海"
p1.age = 21
fmt.Printf("p1=%v\n",p1) // p1={Snial 上海 21}
fmt.Printf("p1=%#v\n",p1) //p1 main.person{name:"Snail",city:"上海",age:21}
}
package main
import "fmt"
type persopn struct {
name string
city string
age int
}
func mian() {
var p2 =new(person)
p2.name = "Snail"
p2.age = 21
p2.city = "北京"
fmt.Printf("%T\n", p2) //*main.person
fmt.Printf("p2=%#v\n", p2) //p2=&main.person{name:"张三", city:"北京", age:20}
}
package main
import "fmt"
type person struct {
name string
city string
age int
}
func main() {
p3 := &person{}
fmt.Printf("%T\n",p3) //*main.person
fmt.Printf("p3=%#v\n",p3) //p3=&main.person{name:"",city:"",age:0}
p3.name = "Snail"
p3.age = 21
p3.city = "上海"
(*p3).age = 20 //这样也是可以的
fmt.Printf("p3=%#v\n",p3) //p3=&main.person{name:"Snail",age:21,city:"上海"}
}
package main
import "fmt"
type person struct {
name string
city stirng
age int
}
func main() {
p4:=person{
name : "Snail",
city : "上海",
age : 21,
}
fmt.Printf("p4=%#v\n",p4) //p4=main.person{name:"Snail",city:"上海",age:18}
}
package main
import "fmt"
type person struct {
nama string
city string
age int
}
func main () {
// 初始化结构体的时候可以简写,也就是初始化的时候不写键,直接写值
p7 := &person{
"Snail",
"上海",
23,
}
//p7=&main.person{name:"Snail",city:"上海",age:23}
fmt.Printf("p7=%#v\n", p7)
}
package main
import "fmt"
type Person struct { //Person结构体 Person类型
string
int
}
func main() {
p1 := Person{
"Snail",
18,
}
fmt.Printf("%#v\n", p1) //main.Person{string:"北京", int:18}
fmt.Println(p1.string, p1.int) //北京 18
}
package main
import "fmt"
type Address struct { //Address地址结构体
Province string
City string
}
type User struct { //User用户结构体
Name string
Gender string
Address Address
}
func main() {
user1 := User{
Name : "Snail",
Gender : "男",
Address : Address{
Province : "安徽",
City:"合肥"
}
}
fmt.Printf("user1=%#v\n", user1)
//user1=main.User{Name:" Snail ", Gender:" 男 ", Address:main.Address{Province:"安徽", City:"合肥"}}
}
package main
import "fmt"
type Address struct { //Address 地址结构体
Province string
City string
}
type User struct { //User 用户结构体
Name string
Gender string
Address
}
func main() {
var user2 User
user2.Name = "张三"
user2.Gender = "男"
user2.Address.Province = "广东" //通过匿名结构体.字段名访问
user2.City = "深圳" //直接访问匿名结构体的字段名
fmt.Printf("user2=%#v\n", user2)
//user2=main.User{Name:"张三", Gender:"男", Address:main.Address{Province:"广东", City:"深圳"}}
}
func (接收者变量 接收者类型) 方法名(参数列表)(返回参数){
函数体
}
package main()
import "fmt"
type Person struct {
name string
age int8
}
func (p Person) printInfo() {
fmt.Printf("姓名:%v 年龄:%v", p.name, p.age) // 姓名:Snail 年龄:23
}
func main() {
p1 := Person{
name: "Snail",
age: 23,
}
p1.printInfo() // 姓名:Snail 年龄:23
}
3、值类型和指针类型接收者
package main
import "fmt"
type Person struct {
name string
age int
}
//值类型接受者
func (p Person) printInfo() {
fmt.Printf("姓名:%v 年龄:%v\n", p.name, p.age) // 姓名:小王子 年龄:25
}
//指针类型接收者
func (p *Person) setInfo(name string, age int) {
p.name = name
p.age = age
}
func main() {
p1 := Person{
name: "小王子",
age: 25,
}
p1.printInfo() // 姓名:小王子 年龄:25
p1.setInfo("张三", 20)
p1.printInfo() // 姓名:张三 年龄:20
}
package main
import "fmt"
type Animal struct { //Animal 动物
name string
}
func (a *Animal) run() {
fmt.Printf("%s 会运动!\n", a.name)
}
type Dog struct { //Dog狗
Age int8
Animal // 通过嵌套匿名结构体实现继承
}
func (d Dog) wang() {
fmt.Printf("%s 会汪汪汪~\n", d.name)
}
func main() {
d1 := Dog{
Age: 4,
Animal: Animal{ //注意嵌套的是结构体指针
name: "阿奇",
},
}
d1.wang() //乐乐会汪汪汪~
d1.run() //乐乐会动!
}
package main
import "fmt"
type Animal struct { //Animal 动物
name string
}
func (a *Animal) run() {
fmt.Printf("%s 会运动!\n", a.name)
}
type Dog struct { //Dog狗
Age int8
*Animal //通过嵌套匿名结构体实现继承
}
func (d *Dog) wang() {
fmt.Printf("%s 会汪汪汪~\n", d.name)
}
func main() {
d1 := &Dog{
Age: 4,
Animal: &Animal{ //注意嵌套的是结构体指针
name: "阿奇",
},
}
d1.wang() //乐乐会汪汪汪~
d1.run() //乐乐会动!
}
在 Go 语言中,接收者的类型可以是任何类型,不仅仅是结构体,任何类型都可以拥有方法。
举个例子,我们基于内置的 int 类型使用 type 关键字可以定义新的自定义类型,然后为我们的自定义类型添加方法。
注意事项: 非本地类型不能定义方法,也就是说我们不能给别的包的类型定义方法。
package main
import "fmt"
type myInt int
func (m myInt) SayHello() {
fmt.Println("Hello, 我是一个 int。")
}
func main() {
var m1 myInt
m1.SayHello() //Hello, 我是一个 int。
m1 = 100
fmt.Printf("%#v %T\n", m1, m1) //100 main.MyInt
}