Golang Module
1~$ mkdir greetings
2~$ cd greetings
3~$ go mod init example.com/greetings
In a text editor, create greetings.go with the following content:
1package greetings
2
3import "fmt"
4
5// Hello returns a greeting for the named person.
6func Hello(name string) string {
7 // Return a greeting that embeds the name in a message.
8 message := fmt.Sprintf("Hi, %v. Welcome!", name)
9 return message
10}
Call it from another module
1~$ cd ..
2~$ mkdir hello
3~$ cd hello
4~$ go mod init example.com/hello
In a text editor, create hello.go with the following content:
1package main
2
3import (
4 "fmt"
5
6 "example.com/greetings"
7)
8
9func main() {
10 // Get a greeting message and print it.
11 message := greetings.Hello("Gladys")
12 fmt.Println(message)
13}
Edit example.com/hello module to use your local example.com/greetings module.
Inside 'hello' directory:
1~$ go mod edit -replace example.com/greetings=../greetings
Then, tidy it
1~$ go mod tidy
The go.mod file in example.com/hello module should now be:
1module example.com/hello
2
3go 1.16
4
5replace example.com/greetings => ../greetings
6
7require example.com/greetings v0.0.0-00010101000000-000000000000
Then, from hello directory, run it.
1~$ go run .
2Hi, Gladys. Welcome!
NOTE: This example was directly taken from https://golang.org/doc/tutorial/create-module so check it there for further details.