Golang - Udemy Flashcards
Learning Golang
<p>Basic program</p>
package main
import “fmt”
func main(){ fmt.Println("Helllo World......") }
fmt.Println => accepts variadic parameters and returns multiple values
numBytes, err := fmt.Println(“hello’, 10, 20, “World”)
fmt.Println(a …args)(b, err)
Keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Operators and Punctuations
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= >= { }
/ «_space; /= «= ++ = := , ;
% »_space; %= »_space;= – ! … . :
&^ &^=
Variable Declaration
x := 10 => declares and assigns a variable
var x int => declares a variable and assign default
you cannot use := outside of function scope but you can use var
var x func foo(){ y := 20 }
x has package level scope
y has function level scope
Default values for Primitive datatypes
Boolean => false int => 0 float => 0.0 string => "" pointers => nil
Variable Types
var x := 10
fmr.Printf(“%T”, x) ==> int
var s := “hello world”
s = x => error since you are going to assign int to string
String Literals
s := hello world "how are you" ?
this will preserve the quotes and and carriage returns
Creating your own Type
var a int
type foo int
var b foo
a = 10 b = 20
a = b => error - unable to assign variable of one type to other
a = int(b) => works fine since we are Converting one type into another
Finding about computer architecture
runtime. GOOS - os (linux)
runtime. GOARCH - architectures - 32/64
String manipulations
s := “ABC”
for idx, v := range s{
fmt.Println(idx, v)
}
output == 0 A 1 B 2 C
Constants
const a = 10
const (
b = 20
c = 30
)
const k int = 200
Packages
two types
- executable
- reusable package
executable packages
- needs to have package main declaration at the start
- needs to have func main()
- produces an executable at the end of the build step.
reusable package can have any name and it does not produce an executable at the end of the build process.
Assigning value to a variable
var card string = “ace of spades”
card := “ace of spades”
both declarations are equivalent
:= is only used when you are creating a new variables and NOT when assigning values to an existing variable (you can use =)
Functions
func () {
}
Arrays
arrays - fixed size
slices - variable size
arr = [] string {"foo", "bar"} arr = append(arr, "baz")
append does not modify the existing array
it creates a new array and assigns values back to arr
Iterating over an array
nums = [] int {10, 20, 30, 40}
for i, num := range nums{
fmt.Prinln(i, num)
}
Creating user defined types
type deck := [] string
we have declared a user defined typed which will accept an array of string
d := deck {“foo”, “bar”}
Adding receivers (functions) to user defined types
type deck := [] string
func (d deck) print (){ for _, card := range d{ fmt.Println(card) } }
d := deck{“foo”, “bar”}
d.print()
(d deck) is called receiver
any variable of type deck will not have access to function called print
receiver setups methods on variables that we create
func ( <var>) (){</var>
}</var>
Structs
type person struct{
firstName string
}
adam := person{“Adam”}
sam := person{firstName: “Sam”}
Structs within structs
type contactInfo struct{
zip string
}
type person struct{
firstName string
contact contactInfo
}
OR type person struct{ firstName string contactInfo }
adam := person{“Adam”}
sam := person{firstName: “Sam”, contact: {
zip: 1234,
}}
Print full struct Info
fmt.Printf(“%+v”, person)
Structs with receiver functions
type person struct{
firstName string
contact contactInfo
}
func (p person) greet(){ fmt.Println("Hello, my name is ", p.firstName) }
adam := person{“Adam”}
adam.greet()
Stucts in Go use pass by value !!!
change this to work
all functions in GO are pass by value i.e. it makes a copy of the object and manipulates it independently.
type person struct{
name string
}
func (p person) changeName(newName string){ p.name = newName }
alice = p{“alice”}
alice. changeName(“Foooooo”)
fmt. Println(alice.name) // alice ??????
funct (p *person) changeName(newName string){
p.name = newName
}
effectively GO will allow you to call the function with the variable and it does not required the pointer
i. e. pts = &alice
ptr. changeName(“Foooo”)
funct (p *person) changeName(newName string){
&p.name = newName
}
Value types and Reference types
Value types
int, float, string, bool, structs
slices, maps, channels, pointers, functions