Golang introduction¶
Golang installation in ubuntu¶
We can install golang with ubuntu snap
like below
$ sudo snap install --classic go
For manual installation read https://golang.org/doc/install. But, I recommend you to use
snap
to skip it.
Test your installation¶
To test the go installation just check the version of golang like below
$ go version
go version go1.14.2 linux/amd64
setup GOPATH¶
open ~/.bash_profile
add below code.
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.bash_profile.
source ~/.bash_profile
Golang "Hello World" program¶
Let's write a golang "hello world" program.
helloWorld.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
how to run the go program ?¶
Golang is compiled language so first we have to compile the program and then execute the byte code. Let's compile the above program
$ go build helloWorld.go
Now, run the bytecode file helloWorld
like below
$ ./helloWorld
hello, world
Hurray!!! we have successfully wrote our first program in golang.
Keywords in Golang¶
Keywords or reserved words in golang are
break, default, func, interface, select, case, defer, go,
map, struct, chan, else, goto, package, switch, const,
fallthrough, if, range, type, continue, for, import, return, var
Predeclared identifiers / Built-in functions¶
append, bool, byte, cap, close, complex, complex64, complex128,
uint16, copy, false, float32, float64, imag, int, int8, int16,
uint32, int32, int64, iota, len, make, new, nil, panic, uint64,
print, println, real, recover, string, true, uint, uint8, uintptr
Identifiers¶
- Identifiers begin with a letter (a letter is every letter in Unicode UTF-8 or _) and followed by 0 or more letters or Unicode digits, like:
X56
,group1
,_x23
,i
,өԑ12
. - The
_
itself is a special identifier, called the blank identifier. It can be used in declarations or variable assignments like any other identifier (and any type can be assigned to it), but its value is discarded, so it cannot be used anymore in the code that follows.