go包应用【我的go学习第八课】
1.环境配置
- gopath设置:我的设置为C:\Users\livingbody\goworkspace
- mod初始化 在定义的mymath的目录下执行 go install mymath
- 设置GO111MODULE为 auto ,go env -w GO111MODULE=auto
set gopath=C:\Users\livingbody\goworkspace
go mod init
go install mymath
go env -w GO111MODULE=auto
2.目录设置
顾名思义,声明函数的框架,例如输入输出等,看例子可秒懂。
└─mymath
go.mod
sqrt.go
3.文件编写
sqrt.go
package mymath
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (x * 2)
}
return z
}
执行go install mymath ,安装mymath自定义库,编写入口 main.go
package main
import (
"fmt"
"mymath"
)
func main() {
fmt.Printf("Hello, world. Sqrt(2)=%v\n", mymath.Sqrt(2))
}
输出
Starting: C:\Users\livingbody\go\bin\dlv.exe dap --check-go-version=false --listen=127.0.0.1:12240 from c:\Users\livingbody\goworkspace
DAP server listening at: 127.0.0.1:12240
Type 'dlv help' for list of commands.
Hello, world. Sqrt(2)=1.414213562373095
Process 17472 has exited with status 0
Detaching
dlv dap (13800) exited with code: 0
本文正在参加技术专题18期-聊聊Go语言框架