golang type的用法理解(陆续补充)
发布日期:2021-05-10 09:46:43 浏览次数:18 分类:精选文章

本文共 4197 字,大约阅读时间需要 13 分钟。

Golang ������������������������������������������������������������

Golang ���������������������������������������������������������������struct���������������interface���������������function���������������������user-defined types������������������������������������������������������������������������������������������������������������������������������������������������������������������

1. ������������������������������������

��� Go ��������������������������������������������������������������������������������������������������������������������������������������������������������� type ������������������������������������������������������������������������������������������������

������������������������������������������ objMap ������������������������������������������ X ��� Y������������������ int ��� string���

type objMap struct {
X int
Y string
}

������������type m objMap ��������������������������������� m ��������������������� objMap ������������������������������������������������������������������������������������������������������������������������������������������������

2. ������������������������

��� Go ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

2.1 ������������

���������������������������

func ������([������������] ���������) [������������] {
���������
}

���������������������������

���������(������)

������������������������������������������ add ��������������������������� int ���������������������������������

func add(x int, y int) int {
return x * y
}

������������������������������������������������������������������������������������������������������������������������������������ public ������������

public func add(x int, y int) int {
return x * y
}

2.2 ������������

���������������������������������������������������������������������������������������������

func ([������] ���������) ���������([������������] ���������) [������������] {
���������
}

���������������������������

������.���������(������)

������������������������������������������ Abs ��������������������������� MyFloat ������������������������������������

type MyFloat float64
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}

���������������������������������������������������������������

func (ax *ageVal) testAge2() int {
*ax = *ax * 8
return 0
}

������������������������������������������������������������

3. ������������������������������

������������������������������������������������������������������������������������������������������������������������������

type Abst interface {
Abs()
}

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

3.1 ���������������������

������������������������������������������������

type MyFloatx float64
type Mapx struct {
X, Y float64
}
func (f MyFloatx) Abs() float64 {
return float64(f * 2)
}
func (v *Mapx) Abs() float64 {
return v.X + v.Y
}
var a MyFloatx = 2
var b Mapx {
X: 2,
Y: 6,
}
a = a.Abs()
b = b.Abs()

���������������������Abs ��������������� MyFloatx ��� Mapx ���������������������������������������

4. ���������������������������������

��� Go ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

type F func(int, int) int

������������������������������������������������������������������������������������������������������������

5. ������

��������������������� Golang ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� Golang ������������������������������

上一篇:微信登陆code拿不到小程序unionid 解决方案
下一篇:vue 导出Excel乱码问题解决方案

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月07日 00时37分01秒