interface
理解 Go interface 的 5 个关键点 | 三月沙
//1
type I interface {
	Get() int
	Set(int)
}
//2
type S struct {
	Age int
}
func (s S) Get() int {
	return s.Age
}
func (s *S) Set(age int) {
	s.Age = age
}
//3
func f(i I) {
	i.Set(10)
	fmt.Println(i.Get())
}
func main() {
	s := S{}
	f(&s) //4
	var i I //声明 i
	i = &s  //赋值 s 到 i
	fmt.Println(i.Get())
} 
 
                
                  
                  
                  
                  
                            
本文通过示例介绍了Go语言中接口的使用,包括定义接口、实现接口以及如何调用接口方法。示例展示了类型S如何隐式实现接口I,并在函数f中通过接口调用Set和Get方法。在main函数中,展示了如何将结构体指针赋值给接口变量并调用接口方法。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					1578
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            