Golang Module
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}
go
Call it from another module
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}
go
Edit example.com/hello
module to use your local example.com/greetings module.
Inside 'hello' directory:
Then, tidy it
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.
NOTE: This example was directly taken from https://golang.org/doc/tutorial/create-module so check it there for further details.