create Golang project with method
1, create new project "test8_method"
$ cd ~/project
$ mkdir test8_method
$ cd test8_method
$ touch method.go
2,add following lines in “method.go”
package mainimport ("fmt"
)type Book struct {author stringpublish uint64
}type Bus struct {startStation stringterminalStation string
}type Number intfunc (book Book) printInfo() {fmt.Println("Book", book)
}func (bus Bus) printInfo() {fmt.Println("Bus:", bus)
}
func (bus *Bus) printInfoP() {fmt.Println("BusP:", bus)
}
func (bus Bus) changeInfo() {bus.startStation = "Unseo" // this line will not change bus.startStation when return
}
func (bus *Bus) changeInfoP() {bus.startStation = "Unseo" // this line will change bus.startStation when return
}func (n Number) selfAdd() (r Number) {r = n + 1return
}func main() {// TEST 1, methodvar _book = Book{"Henry David Thoreau", 1854}var _bus = Bus{"Souel", "Incheon"}var _n = Number(3)_book.printInfo()_bus.printInfo()fmt.Println(_n.selfAdd())// TEST 2, method and pointervar _busP = &Bus{"Incheon", "Souel"}fmt.Println("_bus:", _bus)fmt.Println("_busP:", _busP)_bus.printInfo()_busP.printInfo()_bus.printInfoP()_busP.printInfoP()_bus.changeInfo()_busP.changeInfo()fmt.Println("_bus:", _bus)fmt.Println("_busP:", _busP)_bus.changeInfoP()_busP.changeInfoP()fmt.Println("_bus:", _bus)fmt.Println("_busP:", _busP)
}
3, run “method.go”
go run method.go
you should get result like this:
Book {Henry David Thoreau 1854}
Bus: {Souel Incheon}
4
_bus: {Souel Incheon}
_busP: &{Incheon Souel}
Bus: {Souel Incheon}
Bus: {Incheon Souel}
BusP: &{Souel Incheon}
BusP: &{Incheon Souel}
_bus: {Souel Incheon}
_busP: &{Incheon Souel}
_bus: {Unseo Incheon}
_busP: &{Unseo Souel}
create Golang project with method相关推荐
- create Golang project with interface #1
1, create new project "test9_interface" $ cd ~/project $ mkdir test9_interface $ cd test9_ ...
- SRPG游戏开发(三十一)第八章 游戏中的数据 - 一 创建新项目(Create New Project)
返回总目录 第八章 游戏中的数据(Data in Game) 在之前的章节中,我们进行地图对象的生成,移动等操作. 这一章本来可以进行战斗的编写,不过数据缺失是一个问题. 所以这一章我们先来建立一些数 ...
- fatal: Unable to create ‘D:/project/.git/index.lock‘: File exists
git 命令行报错:fatal: Unable to create 'D:/project/.git/index.lock': File exists 出现这种错误 解决办法: //在git bash ...
- Golang rpc:service/method request ill-formed错误
Golang rpc:service/method request ill-formed错误 在学习golang中rpc的时候遇到了这个错误,无法成功远程调用方法 这里是我想要远程调用的函数的签名部分 ...
- python 正则表达式生成_Python Create Re Project (python自动生成正则表达式项目)
CreateRe Python生成正则表达式模块,此模块用来逆向的生成正则表达式.只需要传入待匹配的字符串,以及预期想要匹配出的结果列表,即可以生成一个正则表达式. 当然目前模块还不够成熟,具体表现在 ...
- create maven android project
2019独角兽企业重金招聘Python工程师标准>>> Getting Started The Android Configurator is an Maven Integratio ...
- golang物联网_使用golang谷歌云平台和grafana监控物联网设备
golang物联网 In this article, we'll be setting up step by step, a monitoring environment for IoT device ...
- golang微信机器人_如何使用Golang从头开始创建Twitter机器人
golang微信机器人 by Kofo Okesola 由Kofo Okesola 如何使用Golang从头开始创建Twitter机器人 (How to create a Twitter bot fr ...
- 使用 VS Code 快速搭建 Golang 开发环境
刚开始接触 Golang 时使用的是 Eclipse + Goclipse 开发环境,但是你懂的,Eclipse的体量有点大,当有一个非常好的 idea 时 Launch and Create t ...
最新文章
- 物体检测中的评价指标【文末赠书】
- Java的API及Object类、String类、字符串缓存区
- Book Review 《构建之法》-2
- [导入]C++ GUi 选择
- 计算机网络网关作用,默认网关的作用
- 面试时如何介绍自己的项目经验
- HTTP协议具体解释
- 12 操作系统第三章 内存管理 非连续分配管理方式 基本分页存储管理 基本分段存储管理 段页式存储管理
- Oracle view 小结片段
- Spring官方阅读笔记-@Controller官方解析与实例(5.0.12)
- 计算机老师任课教师寄语,任课老师寄语大全
- python dataframe索引转成列_如何将 Pandas DataFrame 的索引转换为列
- hpml350服务器安装系统,安装HP ML350
- linux off_t类型的头文件,linux下32位机与64位机基本数据类型长度对比
- 【深入理解JVM】:Java对象的创建、内存布局、访问定位
- ACM 学习笔记(三) 数学入门
- Ubuntu 解压缩
- 从离职之日起,一年内仲裁
- 毕设项目 - 基于SSM的房屋租赁系统(含源码+论文)
- c语言.jpg图片转成数组_如何把PDF图片转成JPG?两个方法快速搞定PDF转图片!