04 Go
04 Go
Ethan Blanton
Department of Computer Science and Engineering
University at Buffalo
Introduction Modules and Packages Slices and Maps Types Pointers Polymorphism Summary References
Writing Go
Go is Unforgiving
Idiomatic Go
Go Modules
A Go module is an installable unit.
The go.mod file gives the module name and its dependencies:
module cse586.messageservice
go 1.19
Packages
E.g., cse586.messageservice/api:
Module cse586.messageservice
Package api
The array has a fixed size, but the slice length can change.
Making Slices
A slice can be taken from an array:
a := [32] int
s := a [:]
Slice Length
Many Go functions and methods operate on slices.
Maps
Ranges
A range expression iterates maps, arrays, slices, strings, and
channels.
for index , value := range variableName {
// index is :
// key for maps
// array index for arrays
// slice index for slices
// unicode character position for strings
}
for value := range channel {
// No index for channels !
}
Strong Typing
Go is strongly typed.
Structures
Go structures are sort of like C structures.
Go Pointers
Method Polymorphism
More than one receiver can implement the same method name.
Argument Polymorphism
Summary
Next Time …
References I
Required Readings
[1] Effective Go. URL: https://go.dev/doc/effective_go.