Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
An Ultimate GopherLabs
Hands-on Labs
Types - slice, map, new, make, struct
• Docker Community Leader , Bangalore
• Author :- lightweight Kubernetes with k3s
with packt Publication
• Gopherlabs – 500+ tutorials
• Okteto – Kubernetes For Developer , Bangalore
Meetup Organizer
Who Am I?
@BiradarSangam
Sangam Biradar
EngineITops.com
https://golang.org/ref/spec
● slice
https://play.golang.org/p/pYxENZqmCOU
Types - slice, map, new, make, struct - Gopherlabs
https://play.golang.org/p/9U46JgUx9zW
0 - a
1 - b
2 - c
3 - g
4 - m
5 - z
● making a Slice
https://play.golang.org/p/ZZVKKlFpSpC
0 Kannada - Shubhodava
1 Konkani - Dev Tuka Boro Dis Divum
2 Korean - Annyunghaseyo
3 Latvian - Labrit
4 Lithuanian – Labas Rytas
5 Macedonian - Dobro utro
6 Malayalam – Namaskaram
7 Maltese – L-Ghodwa t-Tajba
8 Mandarin – Nin hao
9 Marathi - Suprabhat
Kannada - Shubhodava
Konkani - Dev Tuka Boro Dis Divum
Korean - Annyunghaseyo
Latvian – Labrit
Lithuanian – Labas Rytas
Macedonian - Dobro utro
Malayalam – Namaskaram
Maltese – L-Ghodwa t-Tajba
Mandarin – Nin hao
Marathi - Suprabhat
● Slicing a Slices
https://play.golang.org/p/IKvj069CHzv
● making a slice
https://play.golang.org/p/r21LWwwQ9y-
If we think that our slice might grow, we can set a capacity larger than
length. This gives our slice room to grow without golang having to create
a new underlying array every time our slice grows. When the slice
exceeds capacity, then a new underlying array will be created. These arrays
double in size each time they’re created (2, 4, 8, 16) up to a certain point,
and then they scale in some smaller proportion.
● appending to slice
https://play.golang.org/p/dSDiSBTiJC7
● appending beyond capacity to a slice
https://play.golang.org/p/jJdMcszsJRM
If we think that our slice might grow, we can set a capacity larger than
length. This gives our slice room to grow without golang having to
create a new underlying array every time our slice grows. When the
slice exceeds capacity, then a new underlying array will be created.
These arrays typically double in size each time they’re created (2, 4, 8,
16).
● appending slice to a slice
notice the syntax
args...
https://play.golang.org/p/6OIh7zeM5Nj
● delete from Slice
https://play.golang.org/p/Z7H6GdPgDMy
● exercise
● write a program that creates a slice of ints
using shorthand notation then prints the slice
● write a program that creates a slice of strings
using shorthand notation then prints the slice
● create a slice of ints using make;set the length
to 5 and capacity to 10
● append a slice to a slice
● delete an element from a slice.
● Map [key , value]
● map
● basic info
○ key:value
○ maps keys to values
○ called “dictionaries” in some languages
○ built into the language (not an additional library
you must import) so they’re first-class citizens
● Map Keys
○ need to be unique
○ the type used for a key needs to have the
equality operator defined for it
■ the type must allow equals comparisons
■ can’t use these types
● slice
● map
● Maps are Reference Types
○ they behave like pointers
○ when you pass a map variable to a function
■ any changes to that mapped variable
in the function
■ change that original mapped variable
outside the function
● Maps are Not Thread Safe
○ best to avoid using maps concurrently
● Shorthand for Creating Map
https://play.golang.org/p/ptO8KjvdKNt
● adding an entry to a map
https://play.golang.org/p/Tia8HLGK3mD https://play.golang.org/p/0dZnCo440k7
● updating /delete an entry
https://play.golang.org/p/LbZJt2y8UFn https://play.golang.org/p/MAXxKaxgfnw
● comma-ok-idiom
check for existence
comma ok idiom
● comma-ok-idiom
check for existence
comma ok idiom
https://play.golang.org/p/f9LMgiobYMm
● range loop
https://play.golang.org/p/K0G_Etz_uVo
● make a map
https://play.golang.org/p/fTAwe3wEWoL
● exercise
create a program that:
● creates a map using shorthand notation
● adds an entry to the map
● changes an entry in the map
● deletes an entry in the map
● prints all of the entries in the map using range
● prints the len of the map
● uses the comma ok idiom
● make vs new
● make
○ slices, maps, channels
○ allocates memory
○ initializes
■ puts 0 or empty string into values
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a
newly allocated zeroed
value of type T”
https://play.golang.org/p/ij1aXv6J7HE
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make
○ slices, maps, channels
○ allocates memory
○ initializes
■ puts 0 or empty string into values
https://play.golang.org/p/rKcc3Lv8bOY
● zero value
false for booleans, 0 for integers, 0.0 for floats, "" for strings
nil for pointers, functions, interfaces, slices, channels, and maps
● make vs new
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly
allocated zeroed value of type T”
● make
○ slices, maps, channels
○ allocates memory
○ initializes
■ puts 0 or empty string into values
https://play.golang.org/p/hYm8y3AiQDV
● make vs new
● make
○ slices, maps, channels
○ allocates memory
○ initializes
■ puts 0 or empty string into values
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly
allocated zeroed value of type T”
https://play.golang.org/p/zXpIGWTFP5A
● make vs new
● make
○ slices, maps, channels
○ allocates memory
○ initializes
■ puts 0 or empty string into values
https://play.golang.org/p/r77ksucKjSi
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly
allocated zeroed value of type T”
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
● make vs new
https://play.golang.org/p/lhOc5lXQizU
● make vs new
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
https://play.golang.org/p/RRyqZc8q0oI
● make vs new
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
https://play.golang.org/p/Z64HscwjFc_L
● make vs new
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
https://play.golang.org/p/qI1mMyumewG
Types - slice, map, new, make, struct - Gopherlabs
Types - slice, map, new, make, struct - Gopherlabs
● exercise
create a program that:
● declares a variable of type int using new
○ (note: this is not idiomatic go code to create an int this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
create a program that:
● declares a variable of type string using new
○ (note: this is not idiomatic go code to create an string this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
● exercise
create a program that:
● declares a variable of type bool using new
○ (note: this is not idiomatic go code to create an bool this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
● exercise
create a program that:
● declares a variable of type[]int using make
● print out the value of the variable
● true or false:
○ make returned a pointer
create a program that:
● declares a variable of type map[int]string using make
● print out the value of the variable
● true or false:
○ make returned a pointer
● What are the zeroed values for int, string,
bool?
● What are the zeroed values for slice and map
● Explain the difference between make and
new
● struct – grouped fields
create a struct
and initialize it
https://play.golang.org/p/UJ1xEwy4VOP
accessing fields using
dot notation
https://play.golang.org/p/rb1oslM6Y3r
accessing fields using
dot notation
initialize a variable
naming the fields
https://play.golang.org/p/sV4dqi5UJI-
initialize a variable
omitting fields
https://play.golang.org/p/I4hJzlyQurZ
viewing the type
of p1
We have created our own type
which is interesting to think about!
https://play.golang.org/p/FiT64-Cel9_m
taking the address of a struct
p1 is of type *person
https://play.golang.org/p/tSM8aNvoO4n
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly allocated zeroed
value of type T”
● question
Can we use make with a struct?
question
Can we use make with a struct?
no
make is for slices, maps, and channels
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
https://play.golang.org/p/ysOO98tL_3t
https://play.golang.org/p/OBlHXTz3pEO
Types - slice, map, new, make, struct - Gopherlabs
● exercise
create a program that:
● defines a struct type to hold customer info
● initialize two variables using that struct type
● uses dot-notation to print a field from each of
the variables
● changes the value of one of the fields
● prints the changed field
● Can you use new to create a variable of a
struct type?
● question
Can you use make to create a variable of a struct type?
● Review
● slice vs. slicing vs. index access
● slice
○ list
○ mySlice := []int{1, 3, 5, 7, 9, 11}
○ greeting := make([]string, 3, 5)
○ greeting = append(greeting, "Hello")
○ mySlice = append(mySlice, myOtherSlice...)
○ mySlice = append(mySlice[:2], mySlice[3:]...)
● map
○ key, value
■ key type, element type
○ initializing
■ myMap := map[int]string{<entries>}
■ otras := make(map[string]string)
○ adding new entry
■ otras[“new key”] = “new value”
○ changing entry
■ otras[“new key”] = “newer value”
○ delete entry
■ delete(otras, “new key”)
● comma ok idiom
○ if val, ok := myGreeting[2]; ok {
fmt.Println("val: ", val)
fmt.Println("exists: ", exists)
}
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly
allocated zeroed value of
type T”
● struct
○ grouped fields
○ type person struct { name string age int
}
○ p1 := person{name: "James"}
● References
● https://gopherlabs.collabnix.com
● https://godoc.org/
● https://golang.org/doc/
Thanks!
Any questions?
@sangambiradar@BiradarSangamSangam Biradar

More Related Content

Types - slice, map, new, make, struct - Gopherlabs

  • 1. An Ultimate GopherLabs Hands-on Labs Types - slice, map, new, make, struct
  • 2. • Docker Community Leader , Bangalore • Author :- lightweight Kubernetes with k3s with packt Publication • Gopherlabs – 500+ tutorials • Okteto – Kubernetes For Developer , Bangalore Meetup Organizer Who Am I? @BiradarSangam Sangam Biradar EngineITops.com
  • 6. https://play.golang.org/p/9U46JgUx9zW 0 - a 1 - b 2 - c 3 - g 4 - m 5 - z
  • 7. ● making a Slice https://play.golang.org/p/ZZVKKlFpSpC 0 Kannada - Shubhodava 1 Konkani - Dev Tuka Boro Dis Divum 2 Korean - Annyunghaseyo 3 Latvian - Labrit 4 Lithuanian – Labas Rytas 5 Macedonian - Dobro utro 6 Malayalam – Namaskaram 7 Maltese – L-Ghodwa t-Tajba 8 Mandarin – Nin hao 9 Marathi - Suprabhat Kannada - Shubhodava Konkani - Dev Tuka Boro Dis Divum Korean - Annyunghaseyo Latvian – Labrit Lithuanian – Labas Rytas Macedonian - Dobro utro Malayalam – Namaskaram Maltese – L-Ghodwa t-Tajba Mandarin – Nin hao Marathi - Suprabhat
  • 8. ● Slicing a Slices https://play.golang.org/p/IKvj069CHzv
  • 9. ● making a slice https://play.golang.org/p/r21LWwwQ9y- If we think that our slice might grow, we can set a capacity larger than length. This gives our slice room to grow without golang having to create a new underlying array every time our slice grows. When the slice exceeds capacity, then a new underlying array will be created. These arrays double in size each time they’re created (2, 4, 8, 16) up to a certain point, and then they scale in some smaller proportion.
  • 10. ● appending to slice https://play.golang.org/p/dSDiSBTiJC7
  • 11. ● appending beyond capacity to a slice https://play.golang.org/p/jJdMcszsJRM If we think that our slice might grow, we can set a capacity larger than length. This gives our slice room to grow without golang having to create a new underlying array every time our slice grows. When the slice exceeds capacity, then a new underlying array will be created. These arrays typically double in size each time they’re created (2, 4, 8, 16).
  • 12. ● appending slice to a slice notice the syntax args... https://play.golang.org/p/6OIh7zeM5Nj
  • 13. ● delete from Slice https://play.golang.org/p/Z7H6GdPgDMy
  • 14. ● exercise ● write a program that creates a slice of ints using shorthand notation then prints the slice ● write a program that creates a slice of strings using shorthand notation then prints the slice ● create a slice of ints using make;set the length to 5 and capacity to 10 ● append a slice to a slice ● delete an element from a slice.
  • 15. ● Map [key , value]
  • 16. ● map ● basic info ○ key:value ○ maps keys to values ○ called “dictionaries” in some languages ○ built into the language (not an additional library you must import) so they’re first-class citizens ● Map Keys ○ need to be unique ○ the type used for a key needs to have the equality operator defined for it ■ the type must allow equals comparisons ■ can’t use these types ● slice ● map ● Maps are Reference Types ○ they behave like pointers ○ when you pass a map variable to a function ■ any changes to that mapped variable in the function ■ change that original mapped variable outside the function ● Maps are Not Thread Safe ○ best to avoid using maps concurrently
  • 17. ● Shorthand for Creating Map https://play.golang.org/p/ptO8KjvdKNt
  • 18. ● adding an entry to a map https://play.golang.org/p/Tia8HLGK3mD https://play.golang.org/p/0dZnCo440k7
  • 19. ● updating /delete an entry https://play.golang.org/p/LbZJt2y8UFn https://play.golang.org/p/MAXxKaxgfnw
  • 20. ● comma-ok-idiom check for existence comma ok idiom
  • 21. ● comma-ok-idiom check for existence comma ok idiom https://play.golang.org/p/f9LMgiobYMm
  • 23. ● make a map https://play.golang.org/p/fTAwe3wEWoL
  • 24. ● exercise create a program that: ● creates a map using shorthand notation ● adds an entry to the map ● changes an entry in the map ● deletes an entry in the map ● prints all of the entries in the map using range ● prints the len of the map ● uses the comma ok idiom
  • 25. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ○ initializes ■ puts 0 or empty string into values ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T” https://play.golang.org/p/ij1aXv6J7HE
  • 26. ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T” make vs new ● make ○ slices, maps, channels ○ allocates memory ○ initializes ■ puts 0 or empty string into values https://play.golang.org/p/rKcc3Lv8bOY
  • 27. ● zero value false for booleans, 0 for integers, 0.0 for floats, "" for strings nil for pointers, functions, interfaces, slices, channels, and maps
  • 28. ● make vs new ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T” ● make ○ slices, maps, channels ○ allocates memory ○ initializes ■ puts 0 or empty string into values https://play.golang.org/p/hYm8y3AiQDV
  • 29. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ○ initializes ■ puts 0 or empty string into values ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T” https://play.golang.org/p/zXpIGWTFP5A
  • 30. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ○ initializes ■ puts 0 or empty string into values https://play.golang.org/p/r77ksucKjSi ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T”
  • 31. ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values ● make vs new https://play.golang.org/p/lhOc5lXQizU
  • 32. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values https://play.golang.org/p/RRyqZc8q0oI
  • 33. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values https://play.golang.org/p/Z64HscwjFc_L
  • 34. ● make vs new ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values https://play.golang.org/p/qI1mMyumewG
  • 37. ● exercise create a program that: ● declares a variable of type int using new ○ (note: this is not idiomatic go code to create an int this way) ● print out the memory address of the variable ● print out the value of the variable ● true or false: ○ new returned a pointer create a program that: ● declares a variable of type string using new ○ (note: this is not idiomatic go code to create an string this way) ● print out the memory address of the variable ● print out the value of the variable ● true or false: ○ new returned a pointer
  • 38. ● exercise create a program that: ● declares a variable of type bool using new ○ (note: this is not idiomatic go code to create an bool this way) ● print out the memory address of the variable ● print out the value of the variable ● true or false: ○ new returned a pointer
  • 39. ● exercise create a program that: ● declares a variable of type[]int using make ● print out the value of the variable ● true or false: ○ make returned a pointer create a program that: ● declares a variable of type map[int]string using make ● print out the value of the variable ● true or false: ○ make returned a pointer
  • 40. ● What are the zeroed values for int, string, bool? ● What are the zeroed values for slice and map ● Explain the difference between make and new
  • 41. ● struct – grouped fields create a struct and initialize it https://play.golang.org/p/UJ1xEwy4VOP
  • 42. accessing fields using dot notation https://play.golang.org/p/rb1oslM6Y3r
  • 44. initialize a variable naming the fields https://play.golang.org/p/sV4dqi5UJI-
  • 45. initialize a variable omitting fields https://play.golang.org/p/I4hJzlyQurZ
  • 46. viewing the type of p1 We have created our own type which is interesting to think about! https://play.golang.org/p/FiT64-Cel9_m
  • 47. taking the address of a struct p1 is of type *person https://play.golang.org/p/tSM8aNvoO4n
  • 48. ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T”
  • 49. ● question Can we use make with a struct?
  • 50. question Can we use make with a struct? no make is for slices, maps, and channels ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values
  • 54. ● exercise create a program that: ● defines a struct type to hold customer info ● initialize two variables using that struct type ● uses dot-notation to print a field from each of the variables ● changes the value of one of the fields ● prints the changed field ● Can you use new to create a variable of a struct type?
  • 55. ● question Can you use make to create a variable of a struct type?
  • 56. ● Review ● slice vs. slicing vs. index access ● slice ○ list ○ mySlice := []int{1, 3, 5, 7, 9, 11} ○ greeting := make([]string, 3, 5) ○ greeting = append(greeting, "Hello") ○ mySlice = append(mySlice, myOtherSlice...) ○ mySlice = append(mySlice[:2], mySlice[3:]...) ● map ○ key, value ■ key type, element type ○ initializing ■ myMap := map[int]string{<entries>} ■ otras := make(map[string]string) ○ adding new entry ■ otras[“new key”] = “new value” ○ changing entry ■ otras[“new key”] = “newer value” ○ delete entry ■ delete(otras, “new key”) ● comma ok idiom ○ if val, ok := myGreeting[2]; ok { fmt.Println("val: ", val) fmt.Println("exists: ", exists) } ● make ○ slices, maps, channels ○ allocates memory ● initializes ■ puts 0 or empty string into values ● new ○ returns a pointer ■ newly allocated ■ zeroed value ● “returns a pointer to a newly allocated zeroed value of type T” ● struct ○ grouped fields ○ type person struct { name string age int } ○ p1 := person{name: "James"}
  • 57. ● References ● https://gopherlabs.collabnix.com ● https://godoc.org/ ● https://golang.org/doc/