Go does not have classes. However, you can define methods on struct types.

The method receiver appears in its own argument list between the func keyword and the method name.

package main 

import (
"fmt"
"math"
) type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {//相当于给类添加方法
return math.Sqrt(v.X * v.X + v.Y * v.Y)
}
func main() {
v := &Vertex{, }
fmt.Println(v.Abs())
}

A Tour of Go Methods的更多相关文章

  1. A Tour of Go Methods with pointer receivers

    Methods can be associated with a named type or a pointer to a named type. We just saw two Abs method ...

  2. A Tour of Go Methods and Interfaces

    The next group of slides covers methods and interfaces, the constructs that define objects and their ...

  3. A Tour of Go Methods continued

    In fact, you can define a method on any type you define in your package, not just structs. You canno ...

  4. Go Methods and Interfaces

    [Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...

  5. Go指南练习_图像

    https://tour.go-zh.org/methods/25 一.题目描述 还记得之前编写的图片生成器吗?我们再来编写另外一个,不过这次它将会返回一个 image.Image 的实现而非一个数据 ...

  6. Go指南练习_rot13Reader

    https://tour.go-zh.org/methods/23 一.题目描述 有种常见的模式是一个 io.Reader 包装另一个 io.Reader,然后通过某种方式修改其数据流. 例如,gzi ...

  7. Go指南练习_Reader

    https://tour.go-zh.org/methods/22 一.题目描述 实现一个 Reader 类型,它产生一个 ASCII 字符 'A' 的无限流. 二.题目分析 io 包指定了 io.R ...

  8. Go指南练习_错误

    源地址 https://tour.go-zh.org/methods/20 一.题目描述 从之前的练习中复制 Sqrt 函数,修改它使其返回 error 值. Sqrt 接受到一个负数时,应当返回一个 ...

  9. Go指南练习_Stringer

    源地址 https://tour.go-zh.org/methods/18 一.题目描述 通过让 IPAddr 类型实现 fmt.Stringer 来打印点号分隔的地址. 例如,IPAddr{1, 2 ...

随机推荐

  1. 【疯狂Java讲义学习笔记】【数据类型与运算符】

    [学习笔记]1.8bit = 1byte,4byte = 1word.Java中的整型数据有byte(1字节),short(2字节),int(4字节),long(8字节).Java中的浮点数据有flo ...

  2. java main()静态方法

    java main()方法是静态的.意味着不需要new(),就在内存中存在.而且是属于类的,但是对象还是可以调用的. 若干个包含这个静态属性和方法的对象引用都可以指向这个内存区域.这个内存区域发生改变 ...

  3. [itint5]两数积全为1

    http://www.itint5.com/oj/#18 这一题,首先如果直接去算的话,很容易就超出int或者long的表示范围了.那么要利用%的性质,(num * 10 + 1) % a = 10 ...

  4. [Unity菜鸟] Unity Web Player 相关问题 (待完善)

    1. 发布网页版Unity自适应网页大小 发布网页版,Unity3D自适应网页大小.这个问题困扰了我很长时间,今天终于把他解决了,给大家分享一下. 这里用Uinty4.0发布网页版,我去掉了里面的标题 ...

  5. HTML DOM与XML DOM之间,既有区别

    http://kb.cnblogs.com/page/74971/ HTML文档可以使用Core API和HTML API两者: 而XML文档只能使用Core API. 换句话说,HTML与XML重叠 ...

  6. System.Drawing.Design.UITypeEditor自定义控件属性GetEditStyle(ITypeDescriptorContext context),EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  7. Android2.3.7源码结构分析

    对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分.为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析.希望对刚加 ...

  8. wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from SOAP UI (Doc ID 1314946.1)

    wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from ...

  9. 第一部分 Android MediaPlayer 概述

    [IT168 技术文档]本文主要介绍的是Android中很重要也最为复杂的媒体播放器(MediaPlayer)部分的架构.对于Android这样一个完整又相对复杂的系统,一个MediaPlayer功能 ...

  10. JQuery设置与获取RadioButtonList和CheckBoxList的值

    有这样一个问题,要获取ASP.NET控件RadioButtonList的值,首先想到的就是$("#<%=RadioButtonList1.ClientID %>").v ...