go cli
go build -> compiles bunch of go source code files go run -> compiles and executes one or two files go fmt -> formats all code in each file in the current directory go install -> compiles and “installs” a package go get -> downloads raw source code of someone else’s package go test -> runs any tests associated with the project
Package in go is a collection of .go files A module in Go is a collection of related Go packages that are versioned and shared together. Modules are the basic unit of code sharing and dependency management in Go.
package == project == workspace
There are 2 types of packages in golang: Executable package : generates a file that we can run. Executable packages must have a func main(). Reusable package : code used as ‘helpers’/libraries/code dependencies. good place to put reusable logic here.
Q. How do we create an executable package and reusable package? A. We declare “package main” when we want to make sure that building the file spits out an executable.
Q. Then why we declare “package main” and not something like “package folder_name”? A. Because then it wouldn’t be an executable package but a reusable package.
Q. what is import "fmt"
?
A. fmt is another package for formatted I/O. It’s a part of the standard library of Go. Basically, imports all functionality of fmt to our main package.
2 ways to initialize a variable:
-> var identifier data_type = value
-> identifier := value
// this is not dynamic. this as well is done at compile time.
NOTE: You can declare a variable outside of all funcs but can’t initialize it using :=
declaring a function:
func identifier() return_type{
//body
return value_of_return_type
}
NOTE: We can call functions in one file from another file in the SAME package. However, we will have to build the two files with go build
command first.
There are 2(?) builtin sequential data structures in golang:
->slice >can be shrinked/expanded like lists in python (but all elements has to be of same type)
cars := []string{"bugatti", "lamborghini", "ferrari"}
can append to slice using append(slice_name, item_to_be_appended)
cars = append(cars, "tesla")
->array >static. cannot be shrinked/expanded once the size is defined. three methods:
var arr [3]int = [3]int{1,2,3}
arr := [...]int{1,2,3,4}
looping in go:
for i := 0; i<10; i++{
//statements
}
if you want to loop through a slice:
for i, car := range cars{ //here i will be the index number and car will be the actual element
//statements
}
OR
for i := 0; i<len(car); i++{
fmt.Println(car[i])
}
Writing/Reading from file
package used: ioutil
func WriteFile()(filename string, data []byte, perm os.FileMode) error
type: create a custom type using base types like int, string, slices, etc in Object oriented terminology, using type we can “inherit” all properties of the base type.
ex: type deck []string
here deck can be used as slice of strings.
so for example, arr := []string{} is equivalent to arr := deck{}
or if it was type deck int
then a := 45
Receiver: sets up methods on variables that we create
func (reference_variable type_name) function_name(){
//body(includes interaction with reference_variable)
}
here reference_variable works similar to python’s self keyword
NOTE: by convention, the reference_variable is named as first(or first two) letters of the type_name of the receiver