0
点赞
收藏
分享

微信扫一扫

GO中方法和函数的区别

妖妖妈 2022-02-28 阅读 83
package main

import "fmt"

type Student struct {
	Name string
}

func (s Student) test01() {
	fmt.Println(s.Name)
}

func method01(s Student) {
	fmt.Println(s.Name)
}

func main() {
	// 方法需要绑定指定的数据类型
	// 函数不需要

	// 函数名(实参列表)
	// 变量.方法名(实参列表)
	var s Student = Student{"张三"}

	// 调用函数
	method01(s)
	// 调用方法
	s.test01()

}

举报

相关推荐

0 条评论