本文为go官方文档学习笔记
创建module
参考https://golang.org/doc/tutorial/create-module
创建2个module
加路径
实际开发中,路径为url(自己写的module的下载url)
新建文件greeting.go
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
go的func名,首字母大写,可跨package调用
func Hello(name string) string
func 函数名(参数类型)返回类型
:= operator 声明并初始化一个变量
package main
import "fmt"
//import "rsc.io/quote"
import "example.com/greeting"
func main() {
//fmt.Println("Hello, World!")
//fmt.Println(quote.Go())
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
}
当前的greetings是未发布的module
实际开发中,应发布到server上,被调用时联网下载
replace example.com/greetings => ../greetings
require example.com/greetings v1.1.0
module路径
go.mod里replace使用