8 Star 47 Fork 21

Lucky / go-hutool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
19-methods.go 830 Bytes
一键复制 编辑 原始数据 按行查看 历史
package main
import "fmt"
type rect struct {
width, height int
}
//这里的 area 方法有一个接收器(receiver)类型 rect。
func (r *rect) area() int {
return r.width * r.height
}
//可以为值类型或者指针类型的接收器定义方法。这里是一个 值类型接收器的例子。
func (r rect) perim() int {
return 2*r.width + 2*r.height
}
func main() {
r := rect{width: 10, height: 5}
//这里我们调用上面为结构体定义的两个方法。
fmt.Println("area: ", r.area())
fmt.Println("perim:", r.perim())
//Go 自动处理方法调用时的值和指针之间的转化。你可以使 用指针来调用方法来避免在方法调用时产生一个拷贝,或者 让方法能够改变接受的结构体。
rp := &r
fmt.Println("area: ", rp.area())
fmt.Println("perim:", rp.perim())
}
Go
1
https://gitee.com/huangbosbos/go-hutool.git
git@gitee.com:huangbosbos/go-hutool.git
huangbosbos
go-hutool
go-hutool
81f09a275c3b

搜索帮助

53164aa7 5694891 3bd8fe86 5694891