go的结构体与接口

时间:2021-09-02 00:32:54   收藏:0   阅读:7

1.当结构体实现了某个接口的所有方法,结构体指针可转换为接口类型

package main

import "fmt"

type service interface {
	Get(string) (string, error)
	Print()
}

type node struct {
}

func (n *node) Get(in string) (string, error) {
	return in, nil
}
func (n *node) Print() {
	fmt.Println("aaa")
}

func main() {

	var _ service = new(node)

	n := &node{}
	var s service = n
	ret, _ := s.Get("s")
	fmt.Println(ret)

	s.Print()
}

2.结构体内嵌接口类型
2.1结构体内嵌匿名接口

2.2结构体内嵌接口变量

3.结构体内嵌结构体类型
3.1结构体内嵌结构体指针

3.2结构体内嵌结构体

4.接口内嵌接口

5.接口之间强转

6.结构体方法值类型和指针类型

7.匿名结构体与匿名接口

原文:https://www.cnblogs.com/gdut17/p/15212885.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!