Golang - Udemy Flashcards

Learning Golang

1
Q

<p>Basic program</p>

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Keywords

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Operators and Punctuations

A

+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= >= { }
/ &laquo_space; /= «= ++ = := , ;
% &raquo_space; %= &raquo_space;= – ! … . :
&^ &^=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variable Declaration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Default values for Primitive datatypes

A
Boolean   => false
int             => 0
float          => 0.0
string        => ""
pointers    => nil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Variable Types

A

var x := 10
fmr.Printf(“%T”, x) ==> int

var s := “hello world”

s = x => error since you are going to assign int to string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String Literals

A

s := hello world "how are you" ?

this will preserve the quotes and and carriage returns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Creating your own Type

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Finding about computer architecture

A

runtime. GOOS - os (linux)

runtime. GOARCH - architectures - 32/64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

String manipulations

A

s := “ABC”

for idx, v := range s{
fmt.Println(idx, v)
}

output
==
0 A
1 B
2 C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Constants

A

const a = 10

const (
b = 20
c = 30
)

const k int = 200

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Packages

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Assigning value to a variable

A

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 =)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Functions

A

func () {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Arrays

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Iterating over an array

A

nums = [] int {10, 20, 30, 40}

for i, num := range nums{
fmt.Prinln(i, num)
}

17
Q

Creating user defined types

A

type deck := [] string

we have declared a user defined typed which will accept an array of string

d := deck {“foo”, “bar”}

18
Q

Adding receivers (functions) to user defined types

A

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>

19
Q

Structs

A

type person struct{
firstName string
}

adam := person{“Adam”}
sam := person{firstName: “Sam”}

20
Q

Structs within structs

A

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,
}}

21
Q

Print full struct Info

A

fmt.Printf(“%+v”, person)

22
Q

Structs with receiver functions

A

type person struct{
firstName string
contact contactInfo
}

func (p person) greet(){
  fmt.Println("Hello, my name is ", p.firstName)
}

adam := person{“Adam”}
adam.greet()

23
Q

Stucts in Go use pass by value !!!

A

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
}

24
Q

Value types and Reference types

A

Value types

int, float, string, bool, structs

slices, maps, channels, pointers, functions

25
Q

Maps

A
rules
===
keys have to uniform types
values have to be of uniform types
they are pass by reference (so changes will reflect everywhere)

m := map [int]string{ 1: “Monday”, 2: “Tuesday”}
m := make (map[string]string)

m[“new_key”] := “new_value”
delete(m, 1)

26
Q

Interfaces

A

type bot interface{
greet() string
version() string
}

type englishBot struct{}
type spanishBot struct{}

func (b englishBot) greet() string{
  return "Hello"
}
func (b englishBot) version() string{
  return "2.0"
}
func (b spanishBot) greet() string {
  return "Hola"
}
func (b spanishBot) version() string{
  return "1.0"
}
//####
func sayHello(b bot){
  fmt.Println(b.greet())
}
func getVersion(b bot){
  fmt.Println(b.version())
}
func main(){
  alice := englishBot{}
  pablo := spanishBot{}

sayHello(alice)
getVersion(alice)

sayHello(pablo)
getVersion(pablo)
}