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. FileZilla Server 防火墙端口开启设置 windows 2008 win

    入站规则 添加21端口, 程序FileZilla server.exe 出站规则 %SystemRoot%\System32\ftp.exe

  2. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...

  3. ZooKeeper 安装部署及hello world

    ZooKeeper  安装部署及hello world 先给一堆学习文档,方便以后查看官网文档地址大全: OverView(概述)http://zookeeper.apache.org/doc/r3. ...

  4. 安装Hadoop系列 — eclipse plugin插件编译安装配置

    [一].环境参数 eclipse-java-kepler-SR2-linux-gtk-x86_64.tar.gz //现在改为eclipse-jee-kepler-SR2-linux-gtk-x86_ ...

  5. Fisher's exact test( 费希尔精确检验)

    Fisher's exact test[1][2][3] is a statistical significance test used in the analysis ofcontingency t ...

  6. 重载操作符 operator overloading 学习笔记

    重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...

  7. android 页面滑动 ViewFlipper,OnGestureListener,OnTouchListener

    public class Main extends Activity implements OnGestureListener, OnTouchListener { // 一般不直接使用ViewAni ...

  8. Eclipse搭建Struts框架,及一个简单的Struts例子

    一.下载struts2.0.1 http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包含了开发struts2所需的 ...

  9. 普通方式 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果

    首现创建一张表,然后插入1000+万条数据,接下来进行测试. use TTgoif exists (select * from sysobjects where name='Tonge')drop t ...

  10. 【Java】【编译】javac编译源代码时,若源文件使用了别的java源代码的函数,javac会自动关联。

    * 算法第四版自己的alg4.jar似乎有些过时. * 可以引用别的源码文件里的函数