One method
Bind a function to a certain type, usage, normal function declaration, add the bound type after func
package main
import "fmt"//导入包必须使用,否则编译报错
type m_int int //给int给个名
func ( tmp m_int)Add(num m_int)(res m_int){
res = tmp + num
return
}
func main() {//g语言中的 ‘{’ 不能独占一行
var num m_int= 10
res := num.Add(11)
fmt.Println("res=",res);
}
Result The
method of adding members to the structure
package main
import "fmt"//导入包必须使用,否则编译报错
type Student struct {
id int
name string
}
func ( tmp Student)show(){
fmt.Printf("id=%d\n name=%s\n",tmp.id,tmp.name);
}
func main() {//g语言中的 ‘{’ 不能独占一行
stu := Student{15,"小明"}
stu.show()
var pstu* Student = &stu
pstu.show()
}
Result
Note that the type you want to bind cannot be a pointer; for
example, type m_int *int
Two methods-inheritance
package main
import "fmt"//导入包必须使用,否则编译报错
type Student struct {
id int
name string
}
func ( tmp Student)show(){
fmt.Printf("id=%d\n name=%s\n",tmp.id,tmp.name);
}
type SeniorStudent struct{
Student//匿名字段
add string
}
func main() {//g语言中的 ‘{’ 不能独占一行
sstu := SeniorStudent{Student{15,"小明"},"翻斗大街"}
sstu.show()
}
result
Three methods-rewrite
package main
import "fmt"//导入包必须使用,否则编译报错
type Student struct {
id int
name string
}
func ( tmp Student)show(){
fmt.Printf("id=%d\n name=%s\n",tmp.id,tmp.name);
}
type SeniorStudent struct{
Student//匿名字段
add string
}
//方法的重写
func ( tmp SeniorStudent)show(){
fmt.Printf("id=%d\n name=%s\n add=%s\n",tmp.id,tmp.name,tmp.add);
}
func main() {//g语言中的 ‘{’ 不能独占一行
sstu := SeniorStudent{Student{15,"小明"},"翻斗大街"}
sstu.show()
}
Four interfaces-polymorphism
It is equivalent to a pure virtual class of C++. The method has only declarations but no implementation.
package main
import "fmt"//导入包必须使用,否则编译报错
//定义接口
type Person interface{
//接口定义的方法
work()
}
type Student struct {
id int
name string
}
//Student实现方法
func ( tmp Student)work(){
fmt.Printf("Student work\n");
}
type SeniorStudent struct{
Student//匿名字段
add string
}
//SeniorStudent实现方法
func ( tmp SeniorStudent)work(){
fmt.Printf("SeniorStudent work\n");
}
func main() {//g语言中的 ‘{’ 不能独占一行
var per Person
stu := Student{}
sstu := SeniorStudent{}
per = stu
per.work()
per = sstu
per.work()
}
result
Five Interfaces-Inheritance
package main
import "fmt"//导入包必须使用,否则编译报错
//定义接口
type Person interface{
work()
}
type Human interface{//继承了接口,被称为超集
Person//匿名字段,继承work
sleep()
}
type Student struct {
id int
name string
}
func ( tmp Student)work(){
fmt.Printf("Student work\n");
}
//Student实现方法
func ( tmp Student)sleep(){
fmt.Printf("Student sleep\n");
}
func main() {//g语言中的 ‘{’ 不能独占一行
var hu Human
stu := Student{}
hu = stu
hu.work()
hu.sleep()//继承的方法
}
Results A
superset (an interface that inherits an interface) can be converted to a subset, and a subset cannot be converted to a superset
Six air interface
package main
import "fmt"//导入包必须使用,否则编译报错
func show(args ... interface{}){
for _,data := range args{
if value,ok := data.(int); ok == true{//类型查询,断言
fmt.Printf("data = %d\n",value);
}
if value,ok := data.(string); ok == true{
fmt.Printf("data = %s\n",value);
}
}
}
func main() {//g语言中的 ‘{’ 不能独占一行
var i interface{}= 1
fmt.Println("i=",i)
show("123",4,5,"abc")
}
As
you can see, the empty interface can accept any parameter, so you can use it when you don’t know the parameter type.