golang接口/枚举/结构使用示例

1.接口定义

Go 复制代码
// geometry : 定义接口
type geometry interface {
	area() float64  //函数名  返回类型
	perim() float64 //函数名  返回类型
}

2.结构定义

Go 复制代码
// 定义结构area
type rect struct {
	width, height float64 //同类成员写一行,逗号隔开 / 成员名 类型
}

// 定义结构circle
type circle struct {
	radius float64 //成员名 类型
}

3.实现接口方法

在go代码窗口右击弹出菜单,选择Generate ,在弹出的Generate菜单中选择 Implement Methods

先选择结构,后选择接口,然后生成

Go 复制代码
// area() : 实现接口方法area
func (a rect) area() float64 {
	return a.width * a.height
}

// perim :实现接口方法perim
// func (参数名 类型) 函数名() 返回类型
func (a rect) perim() float64 {
	return 2*a.width + 2*a.height
}

func (c circle) area() float64 {
	return math.Pi * c.radius * c.radius
}

func (c circle) perim() float64 {
	return 2 * math.Pi * c.radius
}

4.定义接口调用函数

Go 复制代码
// 封装接口调用方法
// func 函数名(参数名 类型)
func callInterfaceGeometry(g geometry) {
	fmt.Println(g)
	fmt.Println("area:", g.area())
	fmt.Println("perim:", g.perim())
}

5.在主函数中调用并输出

Go 复制代码
func main() {
	fmt.Println("Go接口使用示例")
	//初始化结构,供接口调用
	r := rect{width: 5, height: 5}
	c := circle{radius: 5}
	//调用接口,传入结构
	callInterfaceGeometry(r)
	callInterfaceGeometry(c)
	//使用枚举
	s := 4
	switch s {
	case StateIdle:
		fmt.Println("StateIdle:", 0)
		break
	case StateConnected:
		fmt.Println("StateConnected:", 1)
		break
	case StateError:
		fmt.Println("StateError:", 2)
		break
	case StateRetrying:
		fmt.Println("StateRetrying:", 3)
		break
	case StateRunning:
		fmt.Println("StateRunning:", 4)
		break
	default:
		fmt.Println("StateIdle:", -1)
		break
	}
	fmt.Println("状态:", stateMap[s])
}

输出:

bash 复制代码
Go接口使用示例
{5 5}
area: 25
perim: 20
{5}
area: 78.53981633974483
perim: 31.41592653589793
StateRunning: 4
状态: Running

完整示例:

Go 复制代码
package main

//导入多个包,一行一个
import (
	"fmt"
	"math"
)

// 枚举定义
const (
	StateIdle = iota //iota表示未定义整数,默认为:0
	StateConnected
	StateError
	StateRetrying
	StateRunning
)

var stateMap = map[int]string{
	StateIdle:      "Idle",
	StateConnected: "Connected",
	StateError:     "Error",
	StateRetrying:  "Retrying",
	StateRunning:   "Running",
}

// geometry : 定义接口
type geometry interface {
	area() float64  //函数名  返回类型
	perim() float64 //函数名  返回类型
}

// 定义结构area
type rect struct {
	width, height float64 //同类成员写一行,逗号隔开 / 成员名 类型
}

// 定义结构circle
type circle struct {
	radius float64 //成员名 类型
}

// area() : 实现接口方法area
func (a rect) area() float64 {
	return a.width * a.height
}

// perim :实现接口方法perim
// func (参数名 类型) 函数名() 返回类型
func (a rect) perim() float64 {
	return 2*a.width + 2*a.height
}

func (c circle) area() float64 {
	return math.Pi * c.radius * c.radius
}

func (c circle) perim() float64 {
	return 2 * math.Pi * c.radius
}

// 封装接口调用方法
// func 函数名(参数名 类型)
func callInterfaceGeometry(g geometry) {
	fmt.Println(g)
	fmt.Println("area:", g.area())
	fmt.Println("perim:", g.perim())
}

func main() {
	fmt.Println("Go接口使用示例")
	//初始化结构,供接口调用
	r := rect{width: 5, height: 5}
	c := circle{radius: 5}
	//调用接口,传入结构
	callInterfaceGeometry(r)
	callInterfaceGeometry(c)
	//使用枚举
	s := 4
	switch s {
	case StateIdle:
		fmt.Println("StateIdle:", 0)
		break
	case StateConnected:
		fmt.Println("StateConnected:", 1)
		break
	case StateError:
		fmt.Println("StateError:", 2)
		break
	case StateRetrying:
		fmt.Println("StateRetrying:", 3)
		break
	case StateRunning:
		fmt.Println("StateRunning:", 4)
		break
	default:
		fmt.Println("StateIdle:", -1)
		break
	}
	fmt.Println("状态:", stateMap[s])
}
相关推荐
自由随风飘1 小时前
python 题目练习1~5
开发语言·python
Bony-2 小时前
Go语言完全学习指南 - 从基础到精通------语言基础篇
服务器·开发语言·golang
天若有情6733 小时前
【java EE】IDEA 中创建或迁移 Spring 或 Java EE 项目的核心步骤和注意事项
后端·spring·java-ee·intellij-idea
fl1768313 小时前
基于python的天气预报系统设计和可视化数据分析源码+报告
开发语言·python·数据分析
ACP广源盛139246256734 小时前
(ACP广源盛)GSV6172---MIPI/LVDS 信号转换为 Type-C/DisplayPort 1.4/HDMI 2.0 并集成嵌入式 MCU
c语言·开发语言·单片机·嵌入式硬件·音视频
不穿格子的程序员4 小时前
从零开始刷算法-栈-括号匹配
java·开发语言·
雪域迷影4 小时前
C#中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·http·c#·get
yue0084 小时前
C#类继承
java·开发语言·c#
Want5954 小时前
Python汤姆猫
开发语言·python
大鱼七成饱4 小时前
💥 从崩溃到稳定:我踩过的 Rust Tokio 线程池坑(含代码示例)
后端