1 Star 0 Fork 0

c01dface/s-go-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
methods.go 733 Bytes
一键复制 编辑 原始数据 按行查看 历史
c01dface 提交于 2024-11-26 19:08 . add methods.go
package main
// Go supports methods defined on struct types
import "fmt"
type rect struct {
width, height int
}
// This area method has a receiver type of *rect
func (r *rect) area() int {
return r.width * r.height
}
// value receiver
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 automatically handles conversion between values
// and pointers for method calls. You may want to use a
// pointer receiver type to avoid copying on method calls
// or to allow the method to mutate the receiving struct.
rp := &r
fmt.Println("area: ", rp.area())
fmt.Println("perim: ", rp.perim())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vmalloc/s-go-example.git
git@gitee.com:vmalloc/s-go-example.git
vmalloc
s-go-example
s-go-example
master

搜索帮助