go Tutorial:

创建一个类似的文件格式

Create a Go module

在greetings 的文件夹子里

go mod init <module path>

go mod init greetings 
//这是创建本地的module,不需要上传
go mod init examples.com/greetings
//这是创建远程的module

create function for this module

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
}

Declare a message variable to hold your greeting

In Go, the := operator is a shortcut for declaring and initializing a variable in one line

var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)

------------------------------------------------
 message := fmt.Sprintf("Hi, %v. Welcome!", name)
("Hi, %v. Welcome!", name)
//The %v in the string is a placeholder that Sprintf replaces with name.

Call your code from another module

linux@k82c1:~/go/creat_go_module2/hello$ cat hello.go 
package main

import (
    "fmt"

    "greetings"
    //这里是引入greetings这个module,我们之前创建的。但是由于greetings这个module不在标准库里,所以暂时是找不到的
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}

set the mod file to identifiy where to find the greeting module

只有当module在本地时才需要修改 replace, 重定向引用的greetings

go mod edit -replace greetings=../greetings
// replace the greetings from standard library with the location where we created

当module在网上时,是网上公开的模块可以如下直接应用