2. GoLang
• Critics: There is nothing new in Go
• GoLang Designers: The task of programming
language designer "is consolidation not innovation"
• Less is exponentially more – Rob Pike, Go Designer
• Do Less, Enable More – Russ Cox, Go Tech Lead
3. Why new programming language
• Computers are faster but not software development
• Dependency management is critical
• Dynamically typed languages such as Python and JS
• Garbage collection and parallel are not well
supported by popular system languages
• Multicore computers
4. GoLang
• Go Fast
• Go Simple
• Natively complied (similar to C/C++)
• Static typing
• Fully garbage collection supported
• Support for concurrency and networking
• Rich standard library
• System & Server-side programming
• Scalable
• Open source. Inspired by: C, Pascal, CSP
5. Who
• Robert Griesemer, Ken Thompson, and Rob Pike
started the project in late 2007
• By mid 2008 the language was mostly designed and
the implementation (compiler, runtime) starting to
work
• Ian Lance Taylor and Russ Cox joined in 2008
• Lots of help from many others
• Officially announced in November 2009
• March 2012: Go 1.0
7. Go Usages
• Large scale distributed systems
• Cloud: GCP, Cloud Foundry, Heroku…
• Web development
• Scripting
• System programming
• Open-source project
9. Install Go
• The Go Playground: https://go.dev/play/
• Install Go: https://go.dev/dl/
• Visual Studio Code + extension
• Go packages doc: https://pkg.go.dev/
10. First Go Program
• Every Go file starts with
package
• import: packages use
• func main: entry of a Go
program
• Can use semicolons to
separate statements
(optional)
• go run file.go
package main
import "fmt" //comments
func main() {
fmt.Println("Hello world!")
}
12. fmt Package
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scanln(&name)
fmt.Printf("Hello, %s! n", name)
}
13. Variables
• Go is statically typed similar to C, C++, Pascal
• Implicit type declare
var name = " Hello " //name is string type
• Explicit declare
var name string = " Hello "
• Multiple declare
var x, y int
var (
name string
z int
)
• Multiple init
var x, y int = 1, 2
var (
name = " Hello "
z = 10
)
SIMILAR TO PASCAL
14. Variables
• Shorthand syntax
• Explicit declare
name string := " Hello " //no need var, := instead =
• Implicit declare
name := " Hello " //type is string
• Keyword const, cannot change value
const SIZE int = 10
15. Naming convention
• Camel case
• twoWordString
• iAmAPerson
• Global scope
• File scope
• Export global scope: starts with Uppercase
package main
import "fmt" //comments
var numberOne int = 10
var NumberTwo int = 20
func main() {
fmt.Println("Hello world!")
}
17. Primitive Data Types
package main
import "fmt" //comments
func main() {
var a complex64 = 2 + 3i
fmt.Println(a)
fmt.Println(real(a), imag(a))
a = complex(3, 5)
fmt.Println(a)
}
18. Type conversion
• Downsize or upsize
package main
import "fmt" //comments
func main() {
var i int = 1
var j float32 = float32(i)
j = 5.5
i = int(j)
i = 65
var s string = string(i)
}
19. String conversion
• strconv package
package main
import "fmt"
import "strconv"
func main() {
i := 65
var s string = strconv.Itoa(i)
}
21. Enum
• List of constants
package main
import "fmt"
const (
zero = 0
one = 1
two = "two"
)
func main() {
}
22. Enum
• Using iota (default 0)
package main
import "fmt"
const (
zero = iota
one
two
)
func main() {
}
23. Array
• Fixed size
• Index starts at 0
• Declaration
var arr [3]int
• Initialization
nameList := [3]string {"A", "B", "C"}
• Implicit length
var arr […]int = {1, 2, 3}
• Multi dimensional array
var arr2D [2][3]int
24. Array
• Length of array: len(arr)
• Iterating through the array
for i,value := range arr {
fmt.Println(i, value)
}
• Assign array will copy
25. Slice
• Think of slice as dynamic array (but actually NOT)
var slc []int = {1, 2, 3}
//does not contain …
• Using make
slc := make([]int, 3) //len is 3
slc1 := make([]int, 0, 5)
//len is 0, cap is 5
• Append
slc = append(slc, 4)
• Assign slice will make reference. Use copy to duplicate
dup := make([]int, len(slc))
copy(dup, slc)
• Length: len(slc)
26. Slice Operator
• Slice support operator with the syntax
sliceName[low:high]
fmt.Println(slc[0:2])
fmt.Println(slc[0:3])
fmt.Println(slc[1:])
27. Map
• Similar to dictionary: map[key-type]value-type
student := map[int]string {
1: "A",
2: "B",
3: "C",}
• Using make
student := make(map[int]string)
• Length: len(student)
• Add new element: map[newKey] = value
• Delete: delete(map, key)
• Check existed
val, isExisted := student[5]
//val is empty string
• Slice cannot be key. Array is possible
• Assign will make reference
28. Struct
• Top level
type student struct {
id int
name string
grade float32
}
• Inline
studentA := struct {
id int
name string}{id:123,
name:"abc"}
• Access member using dot operator “.”
• Assign will make copy (same as array)
29. Struct
• Literals
student1 := student {
id: 123,
name: "abc",
grade: 5.5,
}
• Struct name and member must be capitalized to be
exported
30. Struct
• Embedded
type Person struct {
name string
age int
}
type Student struct {
Person //embedded by value
grade float32
}
• Access: student1.Person.name, student1.name
• Tag
type Student struct {
Person
grade float32 `grade must be 0 to 100`
}