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

You cannot define a method on a type from another package, or on a basic type.

package main 

import (
"fmt"
"math"
) type MyFloat float64 func (f MyFloat) Abs() float64 {
if f < {
return float64(-f)
}
return float64(f)
} func main() {
fmt.Println(math.Sqrt2)
f := MyFloat(-math.Sqrt2)
fmt.Println(f.Abs())
}

很像OC中的类别

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

  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

    Go does not have classes. However, you can define methods on struct types. The method receiver appea ...

  3. A Tour of Go Methods and Interfaces

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

  4. A Tour of Go Range continued

    You can skip the index or value by assigning to _. If you only want the index, drop the ", valu ...

  5. A Tour of Go For continued

    As in C or Java, you can leave the pre and post statements empty. package main import "fmt" ...

  6. Go Methods and Interfaces

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

  7. Go指南练习_图像

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

  8. Go指南练习_rot13Reader

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

  9. Go指南练习_Reader

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

随机推荐

  1. HDU2896+AC自动机

    ac自动机 模板题 /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...

  2. Java发送post请求

    package com.baoxiu.test; import java.io.BufferedReader;import java.io.InputStreamReader;import java. ...

  3. Zookeeper + Hadoop + Hbase部署备忘

    网上类似的文章很多,本文只是记录下来备忘.本文分四大步骤: 准备工作.安装zookeeper.安装hadoop.安装hbase,下面分别详细介绍: 一 准备工作 1. 下载 zookeeper.had ...

  4. java jdk自带程序分析(内存分析/线程分析)

    周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令. 1.1.Jstack 1.1   jstack能得到运行jav ...

  5. cctype头文件(字符处理库)的使用

    C++ 中cctype头文件的使用 头文件cctype(字符处理库)中定义了有关字符判断与处理的库函数,使用前要包含头文件: #include <cctype> using namespa ...

  6. Xcode中的iOS工程模板

    1. Application类型 我们大部分的开发工作都是从使用Application类型模板创建iOS程序开始的.该类型共包含7个模板,具体如下所示. Master-Detail Applicati ...

  7. (转)详解LVS负载均衡之三种工作模型原理和10种调度算法

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linuxnx.blog.51cto.com/6676498/1195379 LV ...

  8. Area of a Circle

    Area of a Circle Description: Complete the function circleArea so that it will return the area of a ...

  9. Download interrupted: URL not found.

    Download interrupted: URL not found.   androidURL not found 应该是url被墙了.可以试下:启动 Android SDK Manager ,打 ...

  10. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...