1. Method definition
Go
A method is a special kind of function that, by 接受者
binding 函数
together, can emulate object-oriented behavior.
func (a constru) funName(para paraType,[para2 paraType]) (returnType,[returnType2]) {
}
To bind a function to a type, simply prefix the function name with the type to be bound.
package main
import (
"fmt"
)
type site struct {
name string
age int
}
func (s *site) show() string {
s.name = "https://qiucode.cn"
return s.name
}
func main() {
web := site{
}
str := web.show()
fmt.Println(stra)
}
2. Basic type binding function
package main
import (
"fmt"
)
type intArray []int //定义一个新类型
func (arr *intArray) sum() (sum int) {
for _, num := range *arr {
sum += num
}
return
}
func main() {
num := intArray{
3, 4, 5}
total := num.sum()
fmt.Println(total)
}