06 April 2018 | 5 min read

Get started with Golang

I heard and see lot of articles about Go / Golang which mention lot of advantages of Go compare with other languange such Python, Java, PHP, and C/C++. This is interesting and caught my curiousity.

I am using Fedora 27, let see what we have from repository right now.

$ uname -a
Linux robbinespu 4.15.14-300.fc27.x86_64 #1 SMP Thu Mar 29 16:13:44 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/fedora-release 
Fedora release 27 (Twenty Seven)
$ sudo dnf info golang
Last metadata expiration check: 2:54:31 ago on Fri 06 Apr 2018 04:11:54 PM +08.
Available Packages
Name         : golang
Version      : 1.9.4
Release      : 2.fc27
Arch         : x86_64
Size         : 622 k
Source       : golang-1.9.4-2.fc27.src.rpm
Repo         : updates
Summary      : The Go Programming Language
URL          : http://golang.org/
License      : BSD and Public Domain
Description  : The Go Programming Language.

Fedora Golang packageFedora Golang package - https://apps.fedoraproject.org/packages/golang

Seem right now version 1.10.1 are not available for fedora 27 but available for rawhide (Fedora 28). It should be fine because Google support 2 major version. Now let’s Installing Golang to my machine :

$ sudo dnf install golang
Last metadata expiration check: 2:55:37 ago on Fri 06 Apr 2018 04:11:54 PM +08.
Dependencies resolved.
 Package                                                Arch                                               Version                                                     Repository                                           Size
 golang                                                 x86_64                                             1.9.4-2.fc27                                                updates                                             622 k
Installing dependencies:
 golang-bin                                             x86_64                                             1.9.4-2.fc27                                                updates                                              48 M
 golang-src                                             noarch                                             1.9.4-2.fc27                                                updates                                             5.2 M

Transaction Summary
Install  3 Packages

Total download size: 54 M
Installed size: 221 M
Is this ok [y/N]: y
Downloading Packages:
[MIRROR] golang-src-1.9.4-2.fc27.noarch.rpm: Status code: 404 for http://ftp.cuhk.edu.hk/pub/linux/fedora/updates/27/x86_64/Packages/g/golang-src-1.9.4-2.fc27.noarch.rpm                                                       
[MIRROR] golang-bin-1.9.4-2.fc27.x86_64.rpm: Status code: 404 for http://ftp.cuhk.edu.hk/pub/linux/fedora/updates/27/x86_64/Packages/g/golang-bin-1.9.4-2.fc27.x86_64.rpm                                                       
[MIRROR] golang-1.9.4-2.fc27.x86_64.rpm: Status code: 404 for http://ftp.cuhk.edu.hk/pub/linux/fedora/updates/27/x86_64/Packages/g/golang-1.9.4-2.fc27.x86_64.rpm                                                               
(1/3): golang-1.9.4-2.fc27.x86_64.rpm                                                                                                                                                            155 kB/s | 622 kB     00:04    
(2/3): golang-src-1.9.4-2.fc27.noarch.rpm                                                                                                                                                         97 kB/s | 5.2 MB     00:54    
[MIRROR] golang-bin-1.9.4-2.fc27.x86_64.rpm: Curl error (18): Transferred a partial file for http://mirrors.njupt.edu.cn/fedora/updates/27/x86_64/Packages/g/golang-bin-1.9.4-2.fc27.x86_64.rpm [transfer closed with 36298042 bytes remaining to read]
[MIRROR] golang-bin-1.9.4-2.fc27.x86_64.rpm: Curl error (28): Timeout was reached for http://ftp.yzu.edu.tw/Linux/Fedora/linux/updates/27/x86_64/Packages/g/golang-bin-1.9.4-2.fc27.x86_64.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds]
(3/3): golang-bin-1.9.4-2.fc27.x86_64.rpm                                                                                                                                                        103 kB/s |  48 MB     08:00    
Total                                                                                                                                                                                            115 kB/s |  54 MB     08:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Running scriptlet: golang-1.9.4-2.fc27.x86_64                                                                                                                                                                              1/1 
  Preparing        :                                                                                                                                                                                                         1/1 
  Installing       : golang-src-1.9.4-2.fc27.noarch                                                                                                                                                                          1/3 
  Installing       : golang-bin-1.9.4-2.fc27.x86_64                                                                                                                                                                          2/3 
  Running scriptlet: golang-bin-1.9.4-2.fc27.x86_64                                                                                                                                                                          2/3 
  Installing       : golang-1.9.4-2.fc27.x86_64                                                                                                                                                                              3/3 
  Verifying        : golang-1.9.4-2.fc27.x86_64                                                                                                                                                                              1/3 
  Verifying        : golang-bin-1.9.4-2.fc27.x86_64                                                                                                                                                                          2/3 
  Verifying        : golang-src-1.9.4-2.fc27.noarch                                                                                                                                                                          3/3 

  golang.x86_64 1.9.4-2.fc27                                              golang-bin.x86_64 1.9.4-2.fc27                                              golang-src.noarch 1.9.4-2.fc27                                             


Pfft.. my Maxis telco internet are so slow and it somehow fetching from different location of repository mirror. let’s check our installed Go version via this command

$ go version
go version go1.9.4 linux/amd64

Now verify all configured environment variables using following command. This is the default enviroment right now

$ go env
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build750765771=/tmp/go-build -gno-record-gcc-switches"
The "GOPATH" is used to resolve import statements. The "GOPATH" environment variable is very important for Go workplace. I suggest to change it based on you own need.

Next, we will add the line export GOPATH=$HOME/Workspaces/Golang in the .bashrc.

$ mkdir $HOME/Workspaces/Golang
$ echo 'export GOPATH=$HOME/Workspaces/Golang' >> $HOME/.bashrc
$ source $HOME/.bashrc

If a new terminal started, we should see that GOPATH is now setup to this path. Go workspaces should have src, bin and pkg directory:

$ mkdir -p $GOPATH/src $GOPATH/bin $GOPATH/pkg
$ tree -L 1 $GOPATH
├── bin
├── pkg
└── src

We gonna create our first Hello World

$ mkdir -p $GOPATH/src/hello
$ touch $GOPATH/src/hello/hello.go

Now, edit the file hello.go with your favorite editor and type the following:

package main
import "fmt"
func main() {
	fmt.Println("Hello world, Fedora!")

Save your changes and let quick build the hello.go source with go run arguments

$ go run $GOPATH/src/hello/hello.go
Hello world, Fedora!

If you need to build and generate a compiled binary, use go build

$ cd $GOPATH/src/hello/
$ go build
$ ls -l
total 1824
-rwxrwxr-x 1 rnm rnm 1859889 Apr  7 07:42 hello
-rw-rw-r-- 1 rnm rnm      79 Apr  7 07:32 hello.go
$ ./hello 
Hello world, Fedora!

As you notice, it places a binary in the same directory.

If you need IDE, I would like to suggest using GoLand from Jetbrains products. Goland

I still exploring this Go programming language, so far I found it awesome, easier and fast! I would like to say, I fall in love with Go.

Go is designed and supported by Google the one of the largest cloud infrastructures in the world and it is scaled massively.

Since Go is backed by Google, I believe this will be advantages.

The documentation and license very straight forward and easier to understand. Plus, the Gopher are so cute but I wonder why his eyes like that O_o

Robbi Nespu | Fedora, Golang, Linux, Programming

Discussion and feedback