Go语言学习-实现一个workshop

Creating new Go packages

1、创建一个Go package,叫: MyLib
• Let’s create a Go package called MyLib and use it in our program

2、在go_project文件夹下开启终端,输入指令创建go.mod文件。

go mod init go_project

• Assuming our program resides in go_project/, then type “go mod init go_project” to create a go.mod file

• The go.mod file tells Go that go_project is a module (a module contains one or more packages) – and that our new package can be found in the current directory

3、创建一个目录,叫:MyLib
• Next, create a directory named MyLib

4、在这个目录下,创建文件,叫:Lib1.go
• In MyLib directory, create a file named Lib1.go
在这里插入图片描述

Using our Go packages

5、导入MyLib到我们的程序
• Import MyLib into our program

6、调用MyLib中的方法
• Call the method(s) in MyLib
在这里插入图片描述

Visibility in Go packages

• A package 是最小的单位 of private encapsulation(私有封装) in Go
• All identifiers defined within a package are visible within the
package
• When a package is being imported, only its exported identifiers
can be accessed
• An identifier is exported if it begins with a Capital letter

Package (Example)

在这里插入图片描述

#参考网站
https://go.dev/tour/list

https://www.golang-book.com/books/intro

https://yourbasic.org/golang/

https://www.digitalocean.com/community/tutorials/how-to-use-go-modules

https://github.com/golang/tools/blob/master/gopls/doc/workspace.md

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值