Concise GoLang — Part 1

Vinay
5 min readJun 28, 2020

--

In this series, you will find a concise, hands-on approach to learning the Go language.

In Part 1, we start with the basics of installing Go compiler and running Go programs. This will help prepare us for the interesting projects you are going to build.

Preparation

Download the Go version appropriate for your platform — Windows, Linux, Mac from https://golang.org/dl/. Since I’m running Mac OS, I downloaded https://golang.org/doc/install?download=go1.14.4.darwin-amd64.pkg.

Run the installer and follow the instructions. The default installation path for Mac OS is

/usr/local/go

The installer also adds below directory to he path variable.

/usr/local/go/bin

For other operating systems, follow the instructions from https://golang.org/doc/install

To test if installation is successful, open a new terminal window, and run

go version

You should see result similar to

go version go1.14.4 darwin/amd64

First Executable Program

We will start with a simple program — one which prints “Hello, world.” to the console.

Designate a directory that serves as root for all the programs we write in this series. For e.g:

/Users/vinay/concisego

I’ll refer to this directory path as “workspace” going forward.

Create a directory here called “hello” and change to that directory:

mkdir /Users/vinay/concisego/hello
cd /Users/vinay/concisego/hello

Create a source file with the name hello.go with the following content:

package mainimport "fmt"func main() {
fmt.Println("Hello, world.")
}
  • The first line is the package name. Here we specify the package name as “main”. If we want Go to create an executable file, we need to use this package name.
  • “fmt” is a standard go package that we import to print text
  • main() is a function necessary for an executable program. Function can take zero or more arguments and return zero or more values. main() is just a special function.

To run the program,

go run hello.go

This should result in the following being printed to the console

Hello, world.

To distribute the program, you need to build it. Run the following:

go build

You should see a new executable file called hello in the same directory. Running it gives the same out as above.

First Library

Now instead of an executable package, let us assume you want to create a package with a utility function that just takes input and returns some output. In this example, we’ll create a library with a function that takes a name and returns “Greetings, <name>!!”

Under the workspace directory (/Users/vinay/concisego), create a new directory there called “greeting”. Then change to greeting directory.

Create a file greetname.go with the content:

package greetingimport “fmt”func Greet(name string) string {
return fmt.Sprintf(“Greetings, %s!!\n”, name)
}
  • The package is named “greeting” instead of “main”. That’s because we want a packaged library, not an executable. All files in this package have the same package name in the first line which is the same as the folder name above.
  • Note that there’s no “main()” function either. “Greet” is a library function. It should start with upper case to be available to other packages. “name” is an argument of string type as declared in the function parentheses. The function returns a string type as declared arguments
  • “fmt” is a standard GoLang package. “Sprintf” is a function in the “fmt” package that accepts a format string and arguments and returns the formatted string. In this case we want to greet with any name provided, so we pass the name function argument to Sprintf(). Notice that “Sprintf” is available for us to use because it starts with upper case. Without that, the function would be hidden.

Running “go build” does nothing unlike the hello program above. That’s because there’s no main package. Go compiler would just build but discards the results. It will just serve as a check that the package can be built. We’ll see how to use this utility.

Modules

Go has a concept of module. A module is a collection of packages stored in a file tree with a go.mod file at the root.

Let us create a module for the library we just wrote.

In greeting directory, run this:

go mod init antosara.com/greeting

This creates a file go.mod in the greeting directory with following content

module antosara.com/greetinggo 1.14
  • The first line is module name. There’s typically starts a namespace prefix which in this case is antosara.com or anything you may want to use.
  • The next line is the version of Go used

go build still doesn’t do much. Now let us use this utility in our hello program.

Change to concisego/hello directory. In hello.go file, import the greeting package and add a call to the Greet function.

package mainimport "fmt"
import "antosara.com/greeting"
func main() {
fmt.Println("Hello, world.")
fmt.Println(greeting.Greet("Vinay"))
}

Now, run

go build

You will see an error:

hello.go:4:8: cannot find package "antosara.com/greeting" in any of:/usr/local/go/src/antosara.com/greeting (from $GOROOT)/Users/vinay/go/src/antosara.com/greeting (from $GOPATH)

Go module system expects the imported package to be available in a Version Control System like github. If we had such VCS at antosara.com/greeting, Go would resolve and import package from that location. If it cannot find the module there, Go then tries to find in $GOROOT or $GOPATH locations. But we did not place the greeting package in those locations.

To solve this for now, you could copy go.mod and greetname.go files from greeting directory to the $GOROOT/antosara.com/greeting or $GOPATH/antosara.com/greeting locations above.

Now, in hello directory, run go build. You should see the executable file hello.

Running hello will produce the output below:

Hello, world.
Greetings, Vinay!!

Now, what if you need hello to be a module? In hello directory, run this

go mod init antosara.com/hello

If you run go build, you will see an error:

go: finding module for package antosara.com/greetinghello.go:5:1: cannot find module providing package antosara.com/greeting: unrecognized import path "antosara.com/greeting": reading https://antosara.com/greeting?go-get=1: 404 Not Found

Of course, we don’t have the greeting module at that URL. We can solve that too. Change the content of go.mod to this:

module antosara.com/hellorequire (
antosara.com/greeting v0.0.0
)
replace antosara.com/greeting => ../greetinggo 1.14
  • “require” defines a dependency on greeting module. Usually, if we import a package publicly available into our source code file (hello.go), we don’t have to add this dependency manually in go.mod. When Go sees the import, it automatically pulls the package and adds to go.mod.
  • replace” directs a request to the named package to a local path. In our case, we point to the path of greeting module relative to hello. Absolute path works too.

Now, you should be able to build and run the hello program to get the expected output.

Hello, world.Greetings, Vinay!!

We will explore Go further in Part 2

--

--

Vinay

All accomplishment is transient. Strive unremittingly.