This is the first part of a tutorial that introduces a few fundamental
features of the Go language. If you're just getting started with Go, be sure
to take a look at
Tutorial: Get started with Go, which introduces
the go command, Go modules, and very simple Go code.
In this tutorial you'll create two modules. The first is a library which is intended to be imported by other libraries or applications. The second is a caller application which will use the first.
This tutorial's sequence includes seven brief topics that each illustrate a different part of the language.
Start by creating a Go module. In a module, you collect one or more related packages for a discrete and useful set of functions. For example, you might create a module with packages that have functions for doing financial analysis so that others writing financial applications can use your work. For more about developing modules, see Developing and publishing modules.
Go code is grouped into packages, and packages are grouped into modules. Your module specifies dependencies needed to run your code, including the Go version and the set of other modules it requires.
As you add or improve functionality in your module, you publish new versions of the module. Developers writing code that calls functions in your module can import the module's updated packages and test with the new version before putting it into production use.
Open a command prompt andcd to your home directory.
On Linux or Mac:
cdOn Windows:
cd %HOMEPATH%
greetings directory for your Go module source code.
For example, from your home directory use the following commands:
mkdir greetings cd greetings
go mod init command.
Run the go mod init command, giving it your module path --
here, use example.com/greetings. If you publish a module,
this must be a path from which your module can be downloaded by
Go tools. That would be your code's repository.
For more on naming your module with a module path, see Managing dependencies.
$ go mod init example.com/greetings go: creating new go.mod: module example.com/greetings
The go mod init command creates a go.mod file to track your
code's dependencies. So far, the file includes only the name of your
module and the Go version your code supports. But as you add dependencies,
the go.mod file will list the versions your code depends on. This keeps
builds reproducible and gives you direct control over which module
versions to use.
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
}
This is the first code for your module. It returns a greeting to any caller that asks for one. You'll write code that calls this function in the next step.
In this code, you:
greetings package to collect related functions.
Hello function to return the greeting.
This function takes a name parameter whose type is
string. The function also returns a string.
In Go, a function whose name starts with a capital letter can be
called by a function not in the same package. This is known in Go as
an exported name. For more about exported names, see
Exported names in the
Go tour.
message variable to hold your greeting.
In Go, the := operator is a shortcut for declaring and
initializing a variable in one line (Go uses the value on the right to
determine the variable's type). Taking the long way, you might have
written this as:
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)
fmt package's
Sprintf function to create a greeting message. The
first argument is a format string, and Sprintf substitutes
the name parameter's value for the %v format
verb. Inserting the value of the name parameter completes
the greeting text.
In the next step, you'll call this function from another module.