本文主要是介绍Structs、Methods、Interfaces and Embedding,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
结构体
声明结构体
type rect struct {name stringwidth, height float64
}
创建结构体
r := rect{"rect_struct", 10, 100}
fmt.Println(r)
{rect_struct 10 100}
r := rect{name: "rect_struct", width: 10, height: 100}
fmt.Println(r)
{rect_struct 10 100}
r := rect{name: "rect_struct"}
fmt.Println(r)
{rect_struct 0 0}
取值
r := rect{"rect_struct", 10, 100}
rp := &r
fmt.Println(r)
{rect_struct 10 100}
fmt.Println(r.name)
rect_struct
fmt.Println(rp)
&{rect_struct 10 100}
fmt.Println(rp.name)
rect_struct
赋值
r := rect{"rect_struct", 10, 100}
rp := &r
r.width = 20
fmt.Println(r.width)
rp.width = 200
fmt.Println(rp.width)
20
200
结构体方法
声明结构体和结构体方法
type rect struct {name stringwidth, height float64
}
func (r rect) area() float64 {return r.width * r.height
}func (r rect) perim() float64 {return 2*r.width + 2*r.height
}
这里为结构体值定义方法,好处是方法运行时不会影响到调用方法的结构体,坏处是无法修改结构体的值。
func (r *rect) area() float64 {return r.width * r.height
}func (r *rect) perim() float64 {return 2*r.width + 2*r.height
}
这里为结构体指针定义方法,好处是方法运行时不会复制结构体,坏处是会影响到调用方法的结构体。
调用结构体方法
func (r rect) area() float64 {return r.width * r.height
}func (r *rect) perim() float64 {return 2*r.width + 2*r.height
}
这里定义两个不同的方法,分别对应“值调用者”和“指针调用者”。
r := rect{"rect_struct", 10, 100}
fmt.Println("area:", r.area())
fmt.Println("perim:", r.perim())
area: 1000
perim: 220
rp = &r
fmt.Println("area:", rp.area())
fmt.Println("perim:", rp.perim())
area: 1000
perim: 220
从中可以看出,不管是“值调用者”还是“指针调用者”,对结果毫无影响。
接口
定义接口
type geometry interface {area() float64perim() float64
}
定义结构体
type rect struct {name stringwidth, height float64
}type circle struct {name stringradius float64
}
定义结构体方法
func (r *rect) area() float64 {return r.width * r.height
}func (r *rect) perim() float64 {return 2*r.width + 2*r.height
}func (c *circle) area() float64 {return math.Pi * c.radius * c.radius
}func (c *circle) perim() float64 {return 2 * math.Pi * c.radius
}
从中可以看出,结构体方法与定义的接口一一对应。
调用结构体方法
func measure(g geometry) {fmt.Println(g, "with", "area:", g.area(), "perim:", g.perim())
}
定义方法,用于调用接口方法。
r := rect{"rect_struct", 10, 100}
c := circle{"circle_struct", 10}measure(r)
measure(c)
{rect_struct 10 100} with area: 1000 perim: 220
{circle_struct 10} with area: 314.1592653589793 perim: 62.83185307179586
内嵌
声明内嵌结构体
type base struct {name string
}func (b base) baseDescribe() string {return fmt.Sprintf("base with name: %v", b.name)
}type container struct {title string
}func (c *container) containerDescribe() string {return fmt.Sprintf("container with title: %v", c.title)
}
声明结构体
type rect struct {base*containerwidth, height float64
}func (r rect) area() float64 {return r.width * r.height
}func (r rect) perim() float64 {return 2*r.width + 2*r.height
}type circle struct {base*containerradius float64
}func (c *circle) area() float64 {return math.Pi * c.radius * c.radius
}func (c *circle) perim() float64 {return 2 * math.Pi * c.radius
}
调用结构体
r := rect{base{"rect_struct"}, &container{"rect_container"}, 10, 100}
fmt.Println(r)
fmt.Println(r.name, r.base.name)
fmt.Println(r.title, r.container.title)
fmt.Println(r.baseDescribe(), r.containerDescribe())
fmt.Println("area:", r.area(), "perim:", r.perim())
{{rect_struct} 0xc000096250 10 100}
rect_struct rect_struct
rect_container rect_container
base with name: rect_struct container with title: rect_container
area: 1000 perim: 220
c := circle{base{"circle_struct"}, &container{"circle_container"}, 10}
fmt.Println(c)
fmt.Println(c.name, c.base.name)
fmt.Println(c.title, c.container.title)
fmt.Println(c.baseDescribe(), c.containerDescribe())
fmt.Println("area:", c.area(), "perim:", c.perim())
{{circle_struct} 0xc0000962e0 10}
circle_struct circle_struct
circle_container circle_container
base with name: circle_struct container with title: circle_container
area: 314.1592653589793 perim: 62.83185307179586
调用接口
type describer interface {baseDescribe() stringcontainerDescribe() string
}var rd describer = r
fmt.Println(rd, "with", "baseDescribe:", rd.baseDescribe(), "containerDescribe:", rd.containerDescribe())
var cd describer = c
fmt.Println(cd, "with", "baseDescribe:", cd.baseDescribe(), "containerDescribe:", cd.containerDescribe())
{{rect_struct} 0xc000096250 10 100} with baseDescribe: base with name: rect_struct containerDescribe: container with title: rect_container
{{circle_struct} 0xc0000962e0 10} with baseDescribe: base with name: circle_struct containerDescribe: container with title: circle_container
这篇关于Structs、Methods、Interfaces and Embedding的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!