Call your code from another module
In the previous section, you created a
greetings module. In this section, you'll write code to make
calls to the Hello function in the module you just wrote. You'll
write code you can execute as an application, and which calls code in the
greetings module.
-
Create a
hellodirectory for your Go module source code. This is where you'll write your caller.After you create this directory, you should have both a hello and a greetings directory at the same level in the hierarchy, like so:
<home>/ |-- greetings/ |-- hello/
For example, if your command prompt is in the greetings directory, you could use the following commands:
cd .. mkdir hello cd hello
-
Enable dependency tracking for the code you're about to write.
To enable dependency tracking for your code, run the
go mod initcommand, giving it the name of the module your code will be in.For the purposes of this tutorial, use
example.com/hellofor the module path.$ go mod init example.com/hello go: creating new go.mod: module example.com/hello
- In your text editor, in the hello directory, create a file in which to write your code and call it hello.go.
-
Write code to call the
Hellofunction, then print the function's return value.To do that, paste the following code into hello.go.
package main import ( "fmt" "example.com/greetings" ) func main() { // Get a greeting message and print it. message := greetings.Hello("Gladys") fmt.Println(message) }In this code, you:
-
Declare a
mainpackage. In Go, code executed as an application must be in amainpackage. -
Import two packages:
example.com/greetingsand thefmtpackage. This gives your code access to functions in those packages. Importingexample.com/greetings(the package contained in the module you created earlier) gives you access to theHellofunction. You also importfmt, with functions for handling input and output text (such as printing text to the console). -
Get a greeting by calling the
greetingspackage’sHellofunction.
-
Declare a
-
Edit the
example.com/hellomodule to use your localexample.com/greetingsmodule.For production use, you’d publish the
example.com/greetingsmodule from its repository (with a module path that reflected its published location), where Go tools could find it to download it. For now, because you haven't published the module yet, you need to adapt theexample.com/hellomodule so it can find theexample.com/greetingscode on your local file system.To do that, use the
go mod editcommand to edit theexample.com/hellomodule to redirect Go tools from its module path (where the module isn't) to the local directory (where it is).-
From the command prompt in the hello directory, run the following
command:
$ go mod edit -replace example.com/greetings=../greetings
The command specifies that
example.com/greetingsshould be replaced with../greetingsfor the purpose of locating the dependency. After you run the command, the go.mod file in the hello directory should include areplacedirective:module example.com/hello go 1.16 replace example.com/greetings => ../greetings
-
From the command prompt in the hello directory, run the
go mod tidycommand to synchronize theexample.com/hellomodule's dependencies, adding those required by the code, but not yet tracked in the module.$ go mod tidy go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000
After the command completes, the
example.com/hellomodule's go.mod file should look like this:module example.com/hello go 1.16 replace example.com/greetings => ../greetings require example.com/greetings v0.0.0-00010101000000-000000000000
The command found the local code in the greetings directory, then added a
requiredirective to specify thatexample.com/hellorequiresexample.com/greetings. You created this dependency when you imported thegreetingspackage in hello.go.The number following the module path is a pseudo-version number -- a generated number used in place of a semantic version number (which the module doesn't have yet).
To reference a published module, a go.mod file would typically omit the
replacedirective and use arequiredirective with a tagged version number at the end.require example.com/greetings v1.1.0
For more on version numbers, see Module version numbering.
-
From the command prompt in the hello directory, run the following
command:
-
At the command prompt in the
hellodirectory, run your code to confirm that it works.$ go run . Hi, Gladys. Welcome!
Congrats! You've written two functioning modules.
In the next topic, you'll add some error handling.