Learning Go from Documentation #1 (Hello, World!)

Kuzey Köse
2 min readDec 22, 2021

Installation is simple. Just download the package and install it. I haven’t got any experience on Windows and Linux, but you need to change PATH if you MacOS user. If you don’t, you will see ‘command not found: go’

export PATH=$PATH:/usr/local/go/bin

Let’s create a folder and initialize it with the init command which is ‘go mod init ‘example/hello

$ mkdir hello
$ cd hello
$ go mod init example/hello
$ touch hello.go

go.mod file has to be created in this case. Let’s create a new go file to write some codes. ‘hello.go’ is our first file. Firstly, learning how to execute the function in go with “Hello, World!” function is essential.

// in hello.gopackage main // Declare a main packageimport "fmt" //contains functions for formatting textfunc main() {
fmt.Println("Hello, World!")
}

in terminal, run the command ‘go run .’, you are going to see Hello, World!.

Let’s try to add a new package our code. In pkg.go.dev, we have opportunity to search some package. In documentation suggestions, let’s search a “quote”, and you need to find ‘rsc.io/quote’ package. Then turn back our code and implement it. Implementing is really easy.

package mainimport "fmt"import "rsc.io/quote" // it has to be highlightedfunc main() {
fmt.Println(quote.Go())
}

import “rsc.io/quote” has be highlighted because we don’t take the package yet. We need a go.sum file for use in authenticating the module. In terminal;

$ go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c

the package will be installed and go.sum file created automatically. Let’s run the go function.

$ go run .
Don't communicate by sharing memory, share memory by communicating.

This blog post is personal notes 🙂 If you are more interested in it, you need to check https://go.dev/doc/tutorial/getting-started site.

--

--