Ubuntu installation
64 apt update
65 apt install golang-go
66 go version
root@worker212:~/golang# go version
go version go1.13.8 linux/amd64
First coding with Go
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# cat welcome.go
// Print a friendly greeting
package main
import (
"fmt"
)
func main() {
fmt.Println("Welcome Gophers ☺")
}
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# go run welcome.go
Welcome Gophers ☺
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# ls
welcome.go
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# go build welcome.go
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# ls
welcome welcome.go
root@worker212:~/golang/exercise/Ex_Files_Go_EssT/Exercise Files/Ch01/01_01# ./welcome
Welcome Gophers ☺
Go packages
go get github.com/pkg/errors -> For getting non-builtin package
fmt package
formatted print
It can print map as well
Errorf
Fprintf
strings package
strings.Fields
os package
os.Exit(0)
os.stdout
log
"sync"
var wg sync.WaitGroup
io/ioutil
ioutil.ReadFile(f)
crypto/md5
Language properties
No semicolon at the end of line
It uses zero based indexing for array same as C/C++
Go has garbage collector
Go string supports unicode by default
String in go are immutable similar to C
book[4:11]
book[4:]
book[:4]
+ is used to concatenate two strings
It has int , int8 , int16 , int32 , or int64
Go support untyped variable like python (use := instead of =)
Syntax of if, switch, for are similar to C/C++
Case in switch can be string as well unlike C/C++
No need of break statement at the end of case
Switch can be without condition. In this case, case will have condition
for doesn't have () for condition
Built-in functions
len
Sprintf
Snprintf
Built-in data structure
map
map[string]int{} -> Map with string as key and int as value
map[string]interface -> Map with string as key and any type as value
go get
For installing packages
has built-in checks
Array out of bound panic
It has built-in test
go test
test file name should have suffix _test.go
Test function should have prefix Test
It also adds profiling via option -cpuprofile
Go keywords
range
func
Allows more than one return value
It passes param by reference for default and so, any change in param reflects to the argument value
Arguments can be passed as position or via key-value
nil
similar to C NULL
defer
Defer the action till end of function. For example, defer calling an API
If multiple defer, it is called in reversed order
import
To import a package. Example - fmt
package
type
type Trade struct -> Example
struct
type Trade struct -> Example
It can have function defined
Language allows nested struct -> struct within struct
Built-in Data types
string
int
float64
bool
map
interface
like Java interface
error
panic function
go
For concurrency
chan
For concurrency
var
Can be used to define global variable
select
make
module
Useful links
https://stackoverflow.com/questions/4278430/convert-string-to-integer-type-in-go
https://stackoverflow.com/questions/61536104/ioutil-readfile-no-such-file-or-directory