Episode Post & Video Links: 1, 2, 3, 4 (this post), 5, 6, 7, and 8. Non-linked are in the works! Videos available now on Youtube however, so check em’ out!
If you’d like to go through this material too in book form, I highly suggest “The Go Programming Language” by Alan A.A. Donovan & Brian W. Kernighan. I use it throughout these sessions to provide a guideline. I however add a bunch of other material about IDE’s, development tips n’ tricks and other material.
8:00 Starting the core content with some notes. Composite types, arrays, slices, etc.
11:40 Announcement of my first reload – http://compositecode.blog/2019/02/11/… – second successful time blogged here) of the XPS 15 I have, which – https://youtu.be/f0z1chi4v1Q – actually ended in catastrophe the first time!
14:08 Starting the project for this session.
16:48 Setting up arrays, the things that could be confusing, and setup of our first code for the day. I work through assignment, creation, new vs. comparison, and various other characteristics of working with arrays during this time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fmt.Println("Hello, let's talk composite types.") | |
basketOfStuff := [3]string{"The first string","second","This string."} | |
var zeeValues [2]int | |
for i, v := range basketOfStuff { | |
fmt.Printf("Value %d: %s\n", i, v) | |
} | |
fmt.Println(zeeValues) | |
if zeeValues[0] == zeeValues[1] { | |
fmt.Println("The values are the same, this doesn't instantiate like the `new` keyword.") | |
} else { | |
fmt.Println("The way go appears to instantiate unset variable values, such as in this array is like the `new` keyword instantiation.") | |
} | |
zeeValues[0] = 1 + 52 * 3 | |
zeeValues[1] = 9 | |
fmt.Println(zeeValues[len(zeeValues) – 1]) |
29:36 Creation of a type, called Currency, of type int, setting up constants, and using this kind of like an enumerator to work with code that reads cleaner. Plus of course, all the various things that you might want to, or need for a setup of types, ints, and related composite types like this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Currency int | |
const ( | |
USD Currency = iota | |
CAN | |
EUR | |
GBP | |
JPY | |
NOK | |
SEK | |
DKK | |
) | |
symbol := […]string{USD: "$", CAN: "$", EUR: "€", GBP: "£", JPY:"¥", NOK:"kr", SEK:"kr",DKK:"kr"} | |
fmt.Println(EUR, symbol[EUR]) | |
fmt.Println(JPY, symbol[JPY]) | |
r := […]int{99: –1} | |
r[36] = 425 | |
r[42] = 42 | |
fmt.Println(r[36] + r[42]) | |
fmt.Println(strconv.Itoa(r[36])) |
43:48 Creating an example directly from the aforementioned book enumerating the months of the year. This is a great example I just had to work through it a bit for an example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
months := […]string{1: "January", 2:"February", 3: "March", 4:"April", 12:"December"} | |
for _, s := range months { | |
fmt.Printf("The month: %s\n", s) | |
} | |
var runes []rune | |
for _, r := range "Language: 走" { | |
runes = append(runes, r) | |
} | |
fmt.Printf("%q \n", runes) | |
var x, y []int | |
for i := 0; i < 10; i++ { | |
y = appendInt(x, i) | |
fmt.Printf("%d cap=%d\t%v\n", i, cap(y), y) | |
x = y | |
} |
52:40 Here I start showing, and in the process, doing some learning of my own about runes. I wasn’t really familiar with them before digging in just now!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… the rest of the main.go file is here… | |
var runes []rune | |
for _, r := range "Language: 走" { | |
runes = append(runes, r) | |
} | |
fmt.Printf("%q \n", runes) | |
var x, y []int | |
for i := 0; i < 10; i++ { | |
y = appendInt(x, i) | |
fmt.Printf("%d cap=%d\t%v\n", i, cap(y), y) | |
x = y | |
} | |
} | |
func appendInt(x []int, i int) []int { | |
var z []int | |
zlen := len(x) + 1 | |
if zlen <= cap(x) { | |
z = x[:zlen] | |
} else { | |
zcap := zlen | |
if zcap < 2* len(x) { | |
zcap = 2 * len(x) | |
} | |
z = make([]int, zlen, zcap) | |
copy(z, x) | |
} | |
return z | |
} |
1:09:40 Here I break things down and start a new branch for some additional examples. I also derail off into some other things about meetups and such for a short bit. Skip to the next code bits at the next time point.
1:23:58 From here on to the remainder of the video I work through a few examples of how to setup maps, how make works, and related coding around how to retrieve, set, and otherwise manipulate the maps one you’ve got them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
ages := map[string]int{ | |
"Peterson": 52, | |
"Sally": 22, | |
"Javovia": 15, | |
"Ben": 42, | |
} | |
jobAssociation := make(map[string]string) | |
jobAssociation["Peterson"] = "Engineer" | |
jobAssociation["Sally"] = "CEO" | |
jobAssociation["Jovovia"] = "Gamer" | |
jobAssociation["Ben"] = "Programmer" | |
printAges(ages) | |
printJobAssociations(jobAssociation) | |
fmt.Println(ages) | |
fmt.Println(jobAssociation) | |
fmt.Println(jobAssociation["Jovovia"]) | |
fmt.Println(jobAssociation["Frank"]) // Blank! 😮 | |
fmt.Println(ages["Sally"]) | |
delete(ages, "Sally") | |
fmt.Println(ages) | |
fmt.Println(ages["Sally"]) | |
delete(jobAssociation, "Jovovia") | |
fmt.Println(jobAssociation) | |
fmt.Println(jobAssociation["Jovovia"]) // Blank. | |
ages2 := map[string]int{ | |
"Frank": 52, | |
"Johnson": 22, | |
"Smith": 15, | |
"Jezebelle": 42, | |
} | |
ages3 := map[string]int{ | |
"Frank": 52, | |
"Johnson": 22, | |
"Smith": 15, | |
"Jezebelle": 42, | |
} | |
if equal(ages, ages2) { | |
fmt.Println("Naw, not really equal.") | |
} else { | |
fmt.Println("This is correct, not equal.") | |
} | |
if equal(ages2, ages3) { | |
fmt.Println("True, these are effectively the same map values and keys.") | |
} | |
} | |
func printJobAssociations(associations map[string]string) { | |
for name, job := range associations { | |
fmt.Printf("%s\t%s\n", name, job) | |
} | |
} | |
func printAges(ages map[string]int) { | |
for name, age := range ages { | |
fmt.Printf("%s\t%d\n", name, age) | |
} | |
} | |
func equal(x, y map[string]int) bool { | |
if len(x) != len(y) { | |
return false | |
} | |
for k, xv := range x { | |
if yv, ok := y[k]; !ok || yv != xv { | |
return false | |
} | |
} | |
return true | |
} |
That’s it for this synopsis. Until next episode, happy code thrashing and go coding!
3 thoughts on “Learning Go Episode 4 – Composite Types, Slices, Arrays, Etc.”
Comments are closed.