- Go Tools (https://go.dev/dl)
- verification
go version
- verification
- Visual Studio Code (https://code.visualstudio.com)
- Commence : 9:00 AM
- Tea Break : 10:30 AM (15 mins)
- Lunch Break : 12:30 PM (1 hr)
- Tea Break : 3:00 PM (15 mins)
- Wind up : 5:00 PM
- No powerpoints
- Code & Discuss
- Floor is open for Q&A at all times during the class
- Concurrency
- Managed Concurrency using "Goroutines"
- Very cheap (~4KB)
- Very efficient
- Support is built in the "language" itself
- go "keyword", channel "data type", channel "operator" ( <- ), range & select-case "constructs"
- API support through standard library
- "sync" package
- "sync/atomic" package
- Lightweight
- Compiled to machine code
- Execution Speed (equivalent to c++)
- Simplicity
- ONLY 25 keywords
- No access modifiers (public/private/protected)
- No reference types (everything is a value)
- No class (only structs)
- No inheritance (only composition)
- No exceptions (only errors)
- No "try-catch-finally"
- No implicit type conversion
- No pointer arithmatic
- https://pkg.go.dev/std
Create a build
go build [file_name_1.go] [file_name_2.go] ...
go build -o [binary_name] [file_name_1.go] [file_name_2.go] ...
Build and execute
go run [file_name.go]
- string
- bool
- integers
- int8
- int16
- int32
- int64
- int
- unsigned integers
- uint8
- uint16
- uint32
- uint64
- uint
- floating points
- float32
- float64
- complex
- complex64 ( real[float32] + imaginary[float32] )
- complex128 ( real[float64] + imaginary[float64] )
- alias
- byte (alias for unnsigned int)
- rune (alias for unicode code point)
Data Type | Zero value |
---|---|
int family | 0 |
uint family | 0 |
complex family | (0+0i) |
string | "" (empty string) |
bool | false |
byte | 0 |
interface | nil |
pointer | nil |
function | nil |
struct | struct instance |
- Using the "var" keyword
- memory allocated
- memory initialized with the "zero" value of the data type (by default)
- can be used in package & function scope
- Using ":="
- memory allocated
- memory initialized with the given value
- cannot use in the package scope
- Cannot have unused variables
- Can use ":="
- Can have unused variables
- Cannot use ":="
- No support for function overloading
- Functions can return more than 1 result
- Use "named" results for more than 1 result
- Anonymous Functions
- Anonymous functions cannot have any name
- Anonymous functions have to invoked immediately
- Higher Order Functions (ability to treats functions like data)
- Functions can be assigned as values to variables
- Functions can be passed as arguments to other functions
- Functions can be returned as return values from other functions
go run -gcflags="-m" [filename.go]
go build -gcflags="-m" [filename.go]
go run -gcflags="-l" [filename.go]
go build -gcflags="-l" [filename.go]
- Fixed sized typed collection
- Memory is allocated & initialized automatically
- Arrays are "values"
- Dynamic sized typed collection
- Uses an array internally
- len() => size of the slice
- cap() => capacity of the underlying array
- append() => add new values to the slice
- copy() => copies the data into a new slice
- Typed collection of key/value pairs
- delete() => delete a key
- Errors are just values
- Errors are not "thrown" but returned from a function
- Error values must implement "error" interface (best practice)
- Creating an error
- errors.New()
- fmt.ErrorF()
- Custom type that implements the "error" interface
- Functions whose execution is postponed until the current function exeuction is completed
- Represents the state of the application where the application execution cannot proceed further
- The deferred functions that are scheduled to execute will be executed with the panic occurs
- Creating a panic
- panic() function
- Using the recover() function one can gain access to the error that resulted in a panic
- Arrest the abrupt shutdown of the application
- Any code that has to be versioned and deployed together
- Typically a folder with go.mod file
- Manifest file with the metadata of the module
- name
- repo path (advisable)
- go runtime version
- dependencies
- name
go mod init [module_name]
go build .
go run .
go get [module_name]
go mod tidy
go mod download
go mod vendor
go mod graph
go mod why [module_name]
- internal organization of a module
- Typically folders
- Can be nested
import "[module_name/package_name]"
- like "class" on OO language
- Achieved using OS Threads
- OS Threads are costly resources
- ~2MB of memory (in linux)
- Creating & destrying threads are costly (minimized with Thread Pool)
- Thread context switches are costly
- Semaphore based counter
- Ability to block the execution of a function until the counter becomes 0
go run --race [filename.go]
go build --race [filename.go]
var ch chan int
ch = make(chan int)
ch <- 100
data := <- ch