Created
April 21, 2020 03:35
-
-
Save jloescher/54406f1d8fbd0f573dfe9c6bc738f1b0 to your computer and use it in GitHub Desktop.
Ninja6 Completed
This file contains hidden or 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() { | |
foo() | |
func(){ | |
fmt.Println("Anonymous function ran.") | |
}() | |
func(x int){ | |
fmt.Println("The meaning of life:", x) | |
}(42) | |
} | |
func foo() { | |
fmt.Println("Foo ran") | |
} |
This file contains hidden or 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() { | |
p1 := struct { | |
firstName string | |
lastName string | |
age int | |
}{ | |
firstName: "James", | |
lastName: "Bond", | |
age: 32, | |
} | |
fmt.Println(p1) | |
fmt.Println(p1.firstName, p1.lastName, p1.age) // Accessing fields by dot notation | |
} |
This file contains hidden or 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() { | |
var x [2]string // array with length of 2 | |
x[0] = "Jonathan" | |
x[1] = "Loescher" | |
fmt.Println(x) | |
fmt.Printf("%T", x) | |
} |
This file contains hidden or 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() { | |
ii := []int{1,2,3,4,5,6,7,8,9} | |
s := sum(ii...) | |
fmt.Println("all numbers:", s) | |
s2 := even(sum, ii...) | |
fmt.Println("even numbers:", s2) | |
s3 := odd(sum, ii...) | |
fmt.Println("odd numbers:", s3) | |
} | |
func sum(xi ...int) int { | |
total := 0 | |
for _, v := range xi { | |
total += v | |
} | |
return total | |
} | |
func even(f func(xi ...int) int, vi ...int) int { | |
var yi []int | |
for _, v := range vi { | |
if (v % 2) == 0 { | |
yi = append(yi, v) | |
} | |
} | |
return f(yi...) | |
} | |
func odd(f func(xi ...int) int, vi ...int) int { | |
var yi []int | |
for _, v := range vi { | |
if (v % 2) != 0 { | |
yi = append(yi, v) | |
} | |
} | |
return f(yi...) | |
} |
This file contains hidden or 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() { | |
defer foo() // defer delays the execution to the end of the calling function | |
bar() | |
} | |
func foo() { | |
fmt.Println("foo") | |
} | |
func bar() { | |
fmt.Println("bar") | |
} |
This file contains hidden or 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() { | |
f := func(){ // Assigning a function to a variable. | |
fmt.Println("My first func expression") | |
} | |
f() | |
f1 := func(x int){ | |
fmt.Println("the year big brother started watching:", x) | |
} | |
f1(1984) | |
} |
This file contains hidden or 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" | |
) | |
/* | |
The syntax of a function is the following: | |
func (r receiver) identifier( "parameter(s)" "example:" x int "or" x ...string ) ( "return(s)" "example:" bool int string ) { code } | |
Note: Words in quotes are just for clarity, not part of the syntax. | |
*/ | |
func main() { | |
s1 := fmt.Sprintf("for the item in index") | |
s2 := fmt.Sprintf("we are now adding") | |
s3 := fmt.Sprintf("to the total which is now") | |
xi := []int{2,3,4,5,6,7,8,9} | |
x := sum(s1, s2, s3, xi...) // Since the function is expecting a series of Type int it is necessary to use the ... to expand the slice to Type int | |
fmt.Println("The sum of x is:", x) | |
bar(2) | |
} | |
func sum(s1, s2, s3 string, x ...int) int { // accepts unlimited items of Type int and returns one int additionally variadic parameters much be the final parameter | |
fmt.Println(x) | |
fmt.Printf("%T\n", x) | |
sum := 0 | |
for i,v := range x { | |
sum += v | |
fmt.Println(s1, i, s2, v, s3, sum) | |
} | |
return sum | |
} | |
func bar(x int) { // accepts only one item of Type int | |
fmt.Println(x) | |
fmt.Printf("%T\n", x) | |
} |
This file contains hidden or 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 | |
/* | |
Values can have more than one Type | |
*/ | |
import "fmt" | |
type Person struct { | |
firstName string | |
lastName string | |
} | |
type SecretAgent struct { | |
Person | |
ltk bool | |
} | |
func (s SecretAgent) speak() { | |
fmt.Println("I am", s.firstName, s.lastName, "- the SecretAgent speak") | |
} | |
func (p Person) speak() { | |
fmt.Println("I am", p.firstName, p.lastName, "- the Person speak") | |
} | |
type human interface { | |
speak() // Any Type with the method speak() is also of Type human | |
} | |
func bar(h human) { | |
switch h.(type) { | |
case Person: | |
fmt.Println("I was passed into barrrr", h.(Person).firstName) // Assertion | |
case SecretAgentt: | |
fmt.Println("I was passed into barrrr", h.(SecretAgent).firstName) | |
} | |
fmt.Println("I was passed into bar", h) | |
} | |
func main() { | |
sa1 := SecretAgent{ | |
Person: Person{ | |
firstName: "James", | |
lastName: "Bond", | |
}, | |
ltk: true, | |
} | |
sa2 := SecretAgent{ | |
Person: Person{ | |
firstName: "Miss", | |
lastName: "Moneypenny", | |
}, | |
ltk: false, | |
} | |
p1 := Person{ | |
firstName: "Dr.", | |
lastName: "Yes", | |
} | |
fmt.Println(sa1) | |
fmt.Printf("%T\n", sa1) | |
sa1.speak() | |
sa2.speak() | |
fmt.Println(p1) | |
p1.speak() | |
bar(sa1) | |
bar(sa2) | |
bar(p1) | |
} |
This file contains hidden or 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() { | |
m := map[string]int{ | |
"James": 32, | |
"Miss Moneypenny": 27, | |
} // Create map with a key with a Type of string and a value with a Type of int | |
fmt.Println(m) | |
fmt.Println(m["James"]) | |
fmt.Println(m["Barnabas"]) | |
v, ok := m["Barnabas"] // Getting value which is stored in v and if the value exists which is stored as a boolean in ok | |
fmt.Println(v) | |
fmt.Println(ok) // Will be false as Barnabas is not a valid key in the map | |
if v, ok := m["Barnabas"]; ok { // Only will run if ok is equal to true | |
fmt.Println("This is the if print:", v) | |
} | |
m["Todd"] = 33 // Add Key of Todd with a value of 33 | |
// Looping over a map | |
for k, v := range m { | |
fmt.Printf("Key: %v and Value: %v\n", k, v) | |
} | |
delete(m, "Todd") // delete from map using key, it will delete a key that does not exist. | |
fmt.Println("Deleted Todd from map.") | |
for k, v := range m { | |
fmt.Printf("Key: %v and Value: %v\n", k, v) | |
} | |
// correct way. | |
if _, ok := m["James"]; ok { // underscore "_" tosses the value. Can't have unused variables in golang | |
delete(m, "James") | |
} | |
fmt.Println("Deleted James from map.") | |
for k, v := range m { | |
fmt.Printf("Key: %v and Value: %v\n", k, v) | |
} | |
} |
This file contains hidden or 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" | |
/* | |
The syntax of a function is the following: | |
func (r receiver) identifier( "parameter(s)" "example:" x int "or" x ...string ) ( "return(s)" "example:" bool int string ) { code } | |
Note: Words in quotes are just for clarity, not part of the syntax. | |
*/ | |
type Person struct { | |
firstName string | |
lastName string | |
} | |
type SecretAgent struct { | |
Person | |
ltk bool | |
} | |
func (s SecretAgent) speak() { | |
fmt.Println("I am", s.firstName, s.lastName) | |
} | |
func main() { | |
sa1 := SecretAgent{ | |
Person: Person{ | |
firstName: "James", | |
lastName: "Bond", | |
}, | |
ltk: true, | |
} | |
sa2 := SecretAgent{ | |
Person: Person{ | |
firstName: "Miss", | |
lastName: "Moneypenny", | |
}, | |
ltk: false, | |
} | |
fmt.Println(sa1) | |
sa1.speak() | |
sa2.speak() | |
} |
This file contains hidden or 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() { | |
for i := 33; i <= 122; i++ { | |
fmt.Printf("%v in Hex is %#x and in Ascii is: %#U\n", i, i, i) | |
} | |
} |
This file contains hidden or 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() { | |
x := foo() | |
fmt.Printf("%T\n", x) | |
i := x() | |
fmt.Println(i) | |
} | |
func foo() func() int { | |
return func() int { | |
return 451 | |
} | |
} | |
This file contains hidden or 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" | |
"runtime" | |
) | |
func main() { | |
fmt.Println(runtime.GOOS) | |
fmt.Println(runtime.GOARCH) | |
} |
This file contains hidden or 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" | |
// How to create a slice. | |
func main() { | |
// COMPOSITE LITERAL | |
// 0, 1, 2, 3, 4, 5, 6, 7, 8 | |
x := []int{22,33,44,55,66,34,23,53,45} // Slice, allows you to group together values of the same time | |
fmt.Println(x) | |
fmt.Println(x[4]) // get index position of slice | |
fmt.Println(len(x)) // get length of slice | |
fmt.Println(cap(x)) | |
fmt.Println(x[1:]) // Display the slice starting with index 1 till the end of the slice | |
fmt.Println(x[2:6]) // Display the slice starting at index 2, up to index 6 but not including index 6 | |
fmt.Println("For Loop with Range:") | |
for i, v := range x { // loop over a slice using the range keyword | |
fmt.Printf("Index: %v | Value: %v\n", i, v) | |
} | |
fmt.Println("For Loop without Range:") | |
for i := 0; i < len(x); i++ { // loop over a slice without range keyword | |
fmt.Printf("Index: %v | Value: %v\n", i, x[i]) | |
} | |
// Append to slices | |
x = append(x, 1,2,3,4,5) // append additional values to slice x | |
fmt.Println("This is slice x after appending move values.") | |
fmt.Println(x) | |
y := []int{195, 198, 145, 450, 600} // create slice y | |
x = append(x, y...) // Appending slice y into slice x. | |
/* | |
Note: After slice y use the ... to add all of the values. | |
Otherwise it would try to place the slice in the slice which won't | |
work cause the slice is not of Type slice. | |
*/ | |
fmt.Println(x) | |
// Deleting from slices | |
x = append(x[:9], x[14:]...) // will delete everything between index 8 and 14 | |
fmt.Println(x) | |
fmt.Println("This is data from slice m:") | |
// using make to set slice min and max length | |
m := make([]int, 10, 15) // creates a slice of m with a minimum capacity of 10 and a maximum capacity of 100 | |
fmt.Println(m) | |
fmt.Println(len(m)) // current length of m | |
fmt.Println(cap(m)) // maximum capacity of m | |
m[0] = 10 // set 0 index to 10 | |
m[9] = 100 // set 9 index to 100, 10th position | |
fmt.Println(m) | |
fmt.Println(len(m)) // current length of m | |
fmt.Println(cap(m)) // maximum capacity of m | |
m = append(m, 110, 120, 130, 140, 150) // fill slice to max capacity of 15 | |
fmt.Println(m) | |
fmt.Println(len(m)) // current length of m | |
fmt.Println(cap(m)) // maximum capacity of m | |
m = append(m, 160) // once the slice is past max capacity, it doubles capacity | |
fmt.Println(m) | |
fmt.Println(len(m)) // current length of m | |
fmt.Println(cap(m)) // maximum capacity of m | |
m = append(m[:1], m[9:]...) // keep everything before index position 1 and after 8. | capacity does not dynamically reduce once items remove. | |
fmt.Println(m) | |
fmt.Println(len(m)) // current length of m | |
fmt.Println(cap(m)) // maximum capacity of m | |
// Multi-dimensional slices | |
jb := []string{"James", "Bond", "chocolate", "martini"} // normal slice | |
fmt.Println(jb) | |
mp := []string{"Miss", "Moneypenny", "strawberry", "hazelnut"} // normal slice | |
fmt.Println(mp) | |
xp := [][]string{jb, mp} // multi-dimensional slice with a Type of []string | |
fmt.Println(xp) | |
} |
This file contains hidden or 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" | |
) | |
type Person struct { | |
firstName string | |
lastName string | |
age int | |
} | |
type SecretAgent struct { | |
Person // Pull in Person fields | |
ltk bool | |
} | |
func main() { | |
p1 := Person{ | |
firstName: "James", | |
lastName: "Bond", | |
age: 32, | |
} | |
p2 := Person{ | |
firstName: "Miss", | |
lastName: "Moneypenny", | |
age: 27, | |
} | |
sa1 := SecretAgent{ | |
Person: Person{ | |
firstName: "James", | |
lastName: "Bond", | |
age: 32, | |
}, | |
ltk: true, | |
} | |
fmt.Println(p1) | |
fmt.Println(p2) | |
fmt.Println(sa1) | |
fmt.Println(p1.firstName, p1.lastName, p1.age) // Accessing fields by dot notation | |
fmt.Println(p2.firstName, p2.lastName, p2.age) | |
/* | |
Using dot notation you can access the fields using the fully qualified version seen below in the firstName field. | |
Or since fields are promoted automatically you can access through dot notation the same way as other as seen in the | |
lastName and aqe fields. Below. | |
*/ | |
fmt.Println(sa1.Person.firstName, sa1.lastName, sa1.age, sa1.ltk) | |
} |
This file contains hidden or 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() { | |
// Assign variables with short assignment ":=" | |
x := 42 | |
y := "James Bond" | |
z := true | |
// Print variables on one line | |
fmt.Println(x, y, z) | |
// Print each variable on a new line | |
fmt.Println(x) | |
fmt.Println(y) | |
fmt.Println(z) | |
} |
This file contains hidden or 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" | |
// Define variables in package level. These are zero valued. | |
var x int | |
var y string | |
var z bool | |
func main() { | |
// Print defines variables in function | |
fmt.Println(x) | |
fmt.Println(y) | |
fmt.Println(z) | |
} |
This file contains hidden or 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" | |
// Set variables in package level | |
var x = 42 | |
var y = "James Bond" | |
var z = true | |
func main() { | |
// Combine variables into one string | |
s := fmt.Sprintf("%v %v %v", x, y, z) | |
// Print string | |
fmt.Println(s) | |
// Print Type of s variable | |
fmt.Printf("%T\n", s) | |
} |
This file contains hidden or 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" | |
// Create Type with underlying Type of int | |
type myType int | |
// Create variable of Type that was created with the underlying Type of int | |
var x myType | |
func main() { | |
// Print value of x variable | |
fmt.Println(x) | |
// Print Type of x variable | |
fmt.Printf("%T\n", x) | |
// Set x variable to 42 | |
x = 42 | |
// Print x variable | |
fmt.Println(x) | |
} |
This file contains hidden or 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" | |
// Create Type with underlying Type of int | |
type myType int | |
// Create variable of Type that was created with the underlying Type of int | |
var x myType | |
// Declare a variable y of Type int | |
var y int | |
func main() { | |
fmt.Println(x) // Print value of x variable | |
fmt.Printf("%T\n", x) // Print Type of x variable | |
x = 42 // Set x variable to 42 | |
fmt.Println(x) // Print value of x variable | |
y = int(x) // Convert x to Type int and set value to y variable which is Declared as int | |
fmt.Println(y) // Print value of y variable | |
fmt.Printf("%T\n", y) // Print Type of y variable | |
} |
This file contains hidden or 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() { | |
j := 38 // Initialize j variable with value of 38 | |
/* Using format print, print the number in decimal, binary, and hex. | |
* %d displays decimal value | |
* %b displays binary value | |
* %#x displays hex value | |
* \t inserts a tab character | |
* \n inserts a new line | |
*/ | |
fmt.Printf("%d\t%b\t%#x\n", j, j , j) | |
} |
This file contains hidden or 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() { | |
g := 42 == 38 // equal operator | |
h := 42 <= 38 // less than or equal operator | |
i := 42 >= 38 // greater than or equal operator | |
j := 42 != 38 // not equal operator | |
k := 42 < 38 // less than operator | |
l := 42 > 38 // greater than operator | |
fmt.Println(g, h, i ,j , k, l) // Print variables | |
} |
This file contains hidden or 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" | |
const ( | |
a = 42 // unTyped constant | |
b int = 38 // Typed constant | |
) | |
func main() { | |
fmt.Println(a, b) // print constants | |
} |
This file contains hidden or 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" | |
/* | |
Write a program that | |
- assigns an int to a variable | |
- prints that int in decimal, binary, and hex | |
- shifts the bits of that int over 1 position to the left, and assigns that to a variable | |
- prints that variable in decimal, binary, and hex | |
*/ | |
var ( | |
a = 42 | |
) | |
func main() { | |
fmt.Printf("%d\t%b\t%#x\n", a, a , a) | |
b := a<<1 | |
fmt.Printf("%d\t%b\t%#x\n", b, b , b) | |
} |
This file contains hidden or 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() { | |
s := `Jonathan Loescher` // Raw String Literal | |
fmt.Println(s) | |
} |
This file contains hidden or 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" | |
/* | |
Using iota, create 4 constants for the NEXT 4 years. Print the constant values. | |
*/ | |
const ( | |
y1 = iota + 2020 | |
y2 | |
y3 | |
y4 | |
) | |
func main() { | |
fmt.Println(y1, y2, y3, y4) | |
} |
This file contains hidden or 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" | |
// Print every number from 1 to 10,000 | |
func main() { | |
for i := 1; i <= 10000; i++ { | |
fmt.Println(i) | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Write down what these print: | |
fmt.Println(true && true) | |
fmt.Println(true && false) | |
fmt.Println(true || true) | |
fmt.Println(true || false) | |
fmt.Println(!true) | |
*/ | |
func main() { | |
fmt.Printf("true && true evaluates to %v\n", true && true) | |
fmt.Printf("true && false evaluates to %v\n", true && false) | |
fmt.Printf("true || true evaluates to %v\n", true || true) | |
fmt.Printf("true || false evaluates to %v\n", true || false) | |
fmt.Printf("!true evaluates to %v\n", !true) | |
} |
This file contains hidden or 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" | |
// Print every rune code point of the uppercase alphabet three times. | |
func main() { | |
for i := 65; i <= 90; i++ { | |
fmt.Println(i) | |
for x := 0; x < 3; x++ { | |
fmt.Printf("\t%#U\n", i) | |
} | |
} | |
} |
This file contains hidden or 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 | |
/* | |
Create a for loop using this syntax | |
for condition { } | |
Have it print out the years you have been alive. | |
*/ | |
var ( | |
birthYear = 1981 | |
currentYear = 2020 | |
) | |
func main() { | |
for birthYear <= currentYear { | |
println(birthYear) | |
birthYear++ | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Create a for loop using this syntax | |
for { } | |
Have it print out the years you have been alive. | |
*/ | |
var ( | |
birthYear = 1981 | |
currentYear = 2020 | |
) | |
func main() { | |
for { | |
if birthYear > currentYear { | |
break | |
} | |
fmt.Println(birthYear) | |
birthYear++ | |
} | |
} |
This file contains hidden or 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" | |
// Print out the remainder (modulus) which is found for each number between 10 and 100 when it is divided by 4. | |
func main() { | |
for i := 10; i <= 100; i++ { | |
m := i % 4 | |
fmt.Printf("When %v is devided by 4 the remaining value is %v\n", i, m) | |
} | |
} |
This file contains hidden or 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" | |
const currentYear = 2020 | |
func main() { | |
if currentYear >= 2025 { | |
fmt.Println("Your Rich!") | |
} else { | |
fmt.Println("Your Poor!") | |
} | |
} |
This file contains hidden or 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" | |
const currentYear = 2020 | |
func main() { | |
if currentYear >= 2025 { | |
fmt.Println("Your Rich!") | |
} else if (currentYear < 2025) && (currentYear > 2023) { | |
fmt.Println("In two years or less you will be rich!") | |
} else { | |
fmt.Println("Your Poor!") | |
} | |
} |
This file contains hidden or 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" | |
// Create a program that uses a switch statement with no switch expression specified. | |
func main() { | |
switch { // default is true | |
case 2 == 4: | |
fmt.Println("This is false, won't print") | |
case 2 < 4: | |
fmt.Println("This is true, it will print") | |
} | |
} |
This file contains hidden or 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" | |
// Create a program that uses a switch statement with the switch expression specified as a variable of TYPE string with the IDENTIFIER “favSport”. | |
func main() { | |
favSport := "Bicycling" | |
switch favSport { | |
case "Football": | |
fmt.Println("Your favorite sport is Football.") | |
case "Baseball": | |
fmt.Println("Your favorite sport is Baseball.") | |
case "Basketball": | |
fmt.Println("Your favorite sport is Basketball.") | |
case "Hockey": | |
fmt.Println("Your favorite sport is Hockey.") | |
default: | |
fmt.Printf("Your favorite sport %v is not popular.", favSport) | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Using a COMPOSITE LITERAL: | |
create an ARRAY which holds 5 VALUES of TYPE int | |
assign VALUES to each index position. | |
Range over the array and print the values out. | |
Using format printing | |
print out the TYPE of the array | |
*/ | |
func main() { | |
arr := [5]int{10,20,30} | |
for i, v := range arr { | |
fmt.Printf("Index: %v, Value: %v\n", i, v) | |
} | |
arr[3] = 40 // Add or Change values in an array. | |
arr[4] = 50 | |
for _, v := range arr { | |
fmt.Println(v) | |
} | |
fmt.Printf("%T", arr) | |
} |
This file contains hidden or 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" | |
/* | |
Using the code from the previous example, delete a record from your map. Now print the map out using the “range” loop | |
*/ | |
func main() { | |
m := map[string][]string{ // Create a map with a key of Type string, with a value of Type []string which is a string slice | |
`bond_james`: {`Shaken, not stirred`, `Martinis`, `Women`}, | |
`moneypenny_miss`: {`James Bond`, `Literature`, `Computer Science`}, | |
`no_dr`: {`Being evil`, `Ice cream`, `Sunsets`}, | |
} | |
m[`loescher_jonathan`] = []string{`Jason`, `Prema`, `Bicycling`} // Adding to the map | |
delete(m, `no_dr`) // Delete item by key | |
for k, v := range m { | |
fmt.Printf("Key: %v, %v\n", k, v) | |
for i, v := range v { | |
fmt.Printf("\t\tIndex: %v, Value: %v\n", i, v) | |
} | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Using a COMPOSITE LITERAL: | |
create a SLICE of TYPE int | |
assign 10 VALUES | |
Range over the slice and print the values out. | |
Using format printing | |
print out the TYPE of the slice | |
*/ | |
func main() { | |
s := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100} | |
for _, v := range s { | |
fmt.Println(v) | |
} | |
fmt.Printf("%T", s) | |
} |
This file contains hidden or 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" | |
/* | |
Using the code from the previous example, use SLICING to create the following new slices which are then printed: | |
[42 43 44 45 46] | |
[47 48 49 50 51] | |
[44 45 46 47 48] | |
[43 44 45 46 47] | |
*/ | |
func main() { | |
s := []int{42,43,44,45,46,47,48,49,50,51} | |
fmt.Println(s[:5]) // Prints everything to the 5th position not the 5th positions value | |
fmt.Println(s[5:]) // Prints the 5th positions values and onward | |
fmt.Println(s[2:7]) // Prints the 2nd position up to but not including the 7th position | |
fmt.Println(s[1:6]) // Prints the 1st position up to but not including the 6th position | |
} |
This file contains hidden or 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" | |
/* | |
Follow these steps: | |
start with this slice | |
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51} | |
append to that slice this value | |
52 | |
print out the slice | |
in ONE STATEMENT append to that slice these values | |
53 | |
54 | |
55 | |
print out the slice | |
append to the slice this slice | |
y := []int{56, 57, 58, 59, 60} | |
print out the slice | |
*/ | |
func main() { | |
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51} | |
x = append(x, 52) | |
fmt.Println(x) | |
x = append(x, 53, 54, 55) | |
fmt.Println(x) | |
y := []int{56, 57, 58, 59, 60} | |
x = append(x, y...) | |
fmt.Println(x) | |
} |
This file contains hidden or 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" | |
/* | |
To DELETE from a slice, we use APPEND along with SLICING. For this hands-on exercise, follow these steps: | |
start with this slice | |
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51} | |
use APPEND & SLICING to get these values here which you should ASSIGN to a variable “y” and then print: | |
[42, 43, 44, 48, 49, 50, 51] | |
*/ | |
func main() { | |
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51} | |
y := append(x[:3], x[6:]...) | |
fmt.Println(y) | |
} |
This file contains hidden or 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" | |
/* | |
Create a slice to store the names of all of the states in the United States of America. | |
What is the length of your slice? What is the capacity? Print out all of the values, | |
along with their index position in the slice, without using the range clause. Here is a list of the states: | |
` Alabama`, ` Alaska`, ` Arizona`, ` Arkansas`, ` California`, ` Colorado`, ` Connecticut`, ` Delaware`, ` Florida`, ` Georgia`, ` Hawaii`, ` Idaho`, ` Illinois`, ` Indiana`, ` Iowa`, ` Kansas`, ` Kentucky`, ` Louisiana`, ` Maine`, ` Maryland`, ` Massachusetts`, ` Michigan`, ` Minnesota`, ` Mississippi`, ` Missouri`, ` Montana`, ` Nebraska`, ` Nevada`, ` New Hampshire`, ` New Jersey`, ` New Mexico`, ` New York`, ` North Carolina`, ` North Dakota`, ` Ohio`, ` Oklahoma`, ` Oregon`, ` Pennsylvania`, ` Rhode Island`, ` South Carolina`, ` South Dakota`, ` Tennessee`, ` Texas`, ` Utah`, ` Vermont`, ` Virginia`, ` Washington`, ` West Virginia`, ` Wisconsin`, ` Wyoming`, | |
*/ | |
func main() { | |
states := make([]string, 50, 50) | |
states = []string{ | |
` Alabama`, | |
` Alaska`, | |
` Arizona`, | |
` Arkansas`, | |
` California`, | |
` Colorado`, | |
` Connecticut`, | |
` Delaware`, | |
` Florida`, | |
` Georgia`, | |
` Hawaii`, | |
` Idaho`, | |
` Illinois`, | |
` Indiana`, | |
` Iowa`, | |
` Kansas`, | |
` Kentucky`, | |
` Louisiana`, | |
` Maine`, | |
` Maryland`, | |
` Massachusetts`, | |
` Michigan`, | |
` Minnesota`, | |
` Mississippi`, | |
` Missouri`, | |
` Montana`, | |
` Nebraska`, | |
` Nevada`, | |
` New Hampshire`, | |
` New Jersey`, | |
` New Mexico`, | |
` New York`, | |
` North Carolina`, | |
` North Dakota`, | |
` Ohio`, | |
` Oklahoma`, | |
` Oregon`, | |
` Pennsylvania`, | |
` Rhode Island`, | |
` South Carolina`, | |
` South Dakota`, | |
` Tennessee`, | |
` Texas`, | |
` Utah`, | |
` Vermont`, | |
` Virginia`, | |
` Washington`, | |
` West Virginia`, | |
` Wisconsin`, | |
` Wyoming`, | |
} | |
fmt.Println(len(states)) | |
fmt.Println(cap(states)) | |
for i := 0; i < len(states); i++ { | |
fmt.Printf("Index: %v, Value: %v\n", i, states[i]) | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Create a slice of a slice of string ([][]string). Store the following data in the multi-dimensional slice: | |
"James", "Bond", "Shaken, not stirred" | |
"Miss", "Moneypenny", "Helloooooo, James." | |
Range over the records, then range over the data in each record. | |
*/ | |
func main() { | |
jb := []string{"James", "Bond", "Shaken, not stirred"} | |
mp := []string{"Miss", "Moneypenny", "Helloooooo, James."} | |
x := [][]string{jb, mp} | |
for _, v := range x { | |
fmt.Println(v) | |
for _, v := range v { | |
fmt.Println(v) | |
} | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Create a map with a key of TYPE string which is a person’s “last_first” name, | |
and a value of TYPE []string which stores their favorite things. | |
Store three records in your map. | |
Print out all of the values, along with their index position in the slice. | |
`bond_james`, `Shaken, not stirred`, `Martinis`, `Women` | |
`moneypenny_miss`, `James Bond`, `Literature`, `Computer Science` | |
`no_dr`, `Being evil`, `Ice cream`, `Sunsets` | |
*/ | |
func main() { | |
m := map[string][]string{ // Create a map with a key of Type string, with a value of Type []string which is a string slice | |
`bond_james`: {`Shaken, not stirred`, `Martinis`, `Women`}, | |
`moneypenny_miss`: {`James Bond`, `Literature`, `Computer Science`}, | |
`no_dr`: {`Being evil`, `Ice cream`, `Sunsets`}, | |
} | |
for _, v := range m { | |
for i, v := range v { | |
fmt.Printf("Index: %v, Value: %v\n", i, v) | |
} | |
} | |
for k, v := range m { | |
fmt.Printf("Key: %v\n", k) | |
for i, v := range v { | |
fmt.Printf("\t\tIndex: %v, Value: %v\n", i, v) | |
} | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Using the code from the previous example, add a record to your map. Now print the map out using the “range” loop | |
*/ | |
func main() { | |
m := map[string][]string{ // Create a map with a key of Type string, with a value of Type []string which is a string slice | |
`bond_james`: {`Shaken, not stirred`, `Martinis`, `Women`}, | |
`moneypenny_miss`: {`James Bond`, `Literature`, `Computer Science`}, | |
`no_dr`: {`Being evil`, `Ice cream`, `Sunsets`}, | |
} | |
m[`loescher_jonathan`] = []string{`Jason`, `Prema`, `Bicycling`} // Adding to the map | |
for k, v := range m { | |
fmt.Printf("Key: %v, %v\n", k, v) | |
for i, v := range v { | |
fmt.Printf("\t\tIndex: %v, Value: %v\n", i, v) | |
} | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Create your own type “person” which will have an underlying type of “struct” so that it can store the following data: | |
first name | |
last name | |
favorite ice cream flavors | |
Create two VALUES of TYPE person. Print out the values, ranging over the elements in the slice which stores the favorite flavors. | |
*/ | |
type Person struct { | |
firstName string | |
lastName string | |
iceCreamFlavors []string | |
} | |
func main() { | |
p1 := Person{ | |
firstName: "Jason", | |
lastName: "Nguyen", | |
iceCreamFlavors: []string{"Superman", "Crazy Vanilla"}, | |
} | |
p2 := Person{ | |
firstName: "Jonathan", | |
lastName: "Loescher", | |
iceCreamFlavors: []string{"Raspberry", "Rocky Road Raspberry", "Blueberry Cheesecake"}, | |
} | |
fmt.Println(p1.firstName, p1.lastName, "likes these ice cream flavors:") | |
for i, v := range p1.iceCreamFlavors { | |
fmt.Printf("%v: %v\n", i+1, v) | |
} | |
fmt.Println(p2.firstName, p2.lastName, "likes these ice cream flavors:") | |
for i, v := range p2.iceCreamFlavors { | |
fmt.Printf("%v: %v\n", i+1, v) | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Take the code from the previous exercise, | |
then store the values of type person in a map with the key of last name. | |
Access each value in the map. Print out the values, ranging over the slice. | |
*/ | |
type Person struct { | |
firstName string | |
lastName string | |
iceCreamFlavors []string | |
} | |
func main() { | |
p1 := Person{ | |
firstName: "Jason", | |
lastName: "Nguyen", | |
iceCreamFlavors: []string{"Superman", "Crazy Vanilla"}, | |
} | |
p2 := Person{ | |
firstName: "Jonathan", | |
lastName: "Loescher", | |
iceCreamFlavors: []string{"Raspberry", "Rocky Road Raspberry", "Blueberry Cheesecake"}, | |
} | |
m := map[string]Person{ | |
p1.lastName: p1, | |
p2.lastName: p2, | |
} | |
fmt.Println(m["Nguyen"]) | |
fmt.Println(m["Loescher"]) | |
for _, v := range m { | |
fmt.Printf("%v %v likes the following ice cream flavors:", v.firstName, v.lastName) | |
for _, v := range v.iceCreamFlavors { | |
fmt.Println(v) | |
} | |
} | |
} |
This file contains hidden or 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" | |
/* | |
Create a new type: vehicle. | |
- The underlying type is a struct. | |
- The fields: | |
- doors | |
- color | |
- Create two new types: truck & sedan. | |
- The underlying type of each of these new types is a struct. | |
- Embed the “vehicle” type in both truck & sedan. | |
- Give truck the field “fourWheel” which will be set to bool. | |
- Give sedan the field “luxury” which will be set to bool. solution | |
Using the vehicle, truck, and sedan structs: | |
- using a composite literal, create a value of type truck and assign values to the fields; | |
- using a composite literal, create a value of type sedan and assign values to the fields. | |
Print out each of these values. | |
Print out a single field from each of these values. | |
*/ | |
type Vehicle struct { | |
doors int | |
color string | |
} | |
type Truck struct { | |
Vehicle | |
fourWheel bool | |
} | |
type Sedan struct { | |
Vehicle | |
luxury bool | |
} | |
func main() { | |
v1 := Truck{ | |
Vehicle: Vehicle{ | |
doors: 2, | |
color: "Gun Metal", | |
}, | |
fourWheel: true, | |
} | |
v2 := Sedan{ | |
Vehicle: Vehicle{ | |
doors: 4, | |
color: "Blue", | |
}, | |
luxury: true, | |
} | |
fmt.Println(v1) | |
fmt.Println(v2) | |
fmt.Println(v1.fourWheel) | |
fmt.Println(v2.doors) | |
} |
This file contains hidden or 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" | |
// Create and use an anonymous struct. | |
func main() { | |
p1 := struct { | |
firstName string | |
lastName string | |
age int | |
}{ | |
firstName: "Joanthan", | |
lastName: "Loescher", | |
age: 38, | |
} | |
fmt.Printf("%v %v is %v", p1.firstName, p1.lastName, p1.age) | |
} |
This file contains hidden or 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" | |
/* | |
Hands on exercise | |
create a func with the identifier foo that returns an int | |
create a func with the identifier bar that returns an int and a string | |
call both funcs | |
print out their results | |
*/ | |
func main() { | |
f := foo() | |
fmt.Println(f) | |
b, b2 := bar() | |
fmt.Println(b, b2) | |
} | |
func foo() int { | |
return 43 | |
} | |
func bar() (int, string) { | |
return 43, "This is from bar." | |
} |
This file contains hidden or 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" | |
/* | |
Closure is when we have “enclosed” the scope of a variable in some code block. | |
For this hands-on exercise, create a func which “encloses” the scope of a variable: | |
*/ | |
func main() { | |
x := 10 | |
f := func() int { | |
x := 30 | |
return x | |
} | |
fmt.Println("This is x:", x) | |
fmt.Println("This is y:", f()) | |
} |
This file contains hidden or 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 | |
/* | |
The best way to learn is to teach. For this hands-on exercise, | |
- choose one of the above exercises, or use the recursion example of factorial | |
- download, install, and get it running | |
- https://obsproject.com/ | |
- record a video of YOU teaching the topic | |
- upload the video to youtube | |
- share the video on twitter and tag me in it ( https://twitter.com/Todd_McLeod ) so that I can see it! | |
*/ | |
func main() { | |
} |
This file contains hidden or 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" | |
/* | |
create a func with the identifier foo that | |
- takes in a variadic parameter of type int | |
- pass in a value of type []int into your func (unfurl the []int) | |
- returns the sum of all values of type int passed in | |
create a func with the identifier bar that | |
- takes in a parameter of type []int | |
- returns the sum of all values of type int passed in | |
*/ | |
func main() { | |
xi := []int{1,2,3,4,5,6,7,8,9} | |
sum := foo(xi...) | |
fmt.Println("Printing from foo()", sum) | |
sum2 := bar(xi) | |
fmt.Println("Printing from bar()", sum2) | |
} | |
func foo(x ...int) int { | |
var total int | |
for _, v := range x { | |
total += v | |
} | |
return total | |
} | |
func bar(x []int) int { | |
var total int | |
for _, v := range x { | |
total += v | |
} | |
return total | |
} |
This file contains hidden or 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" | |
// Use the “defer” keyword to show that a deferred func runs after the func containing it exits. | |
func main() { | |
xi := []int{1,2,3,4,5,6,7,8,9} | |
defer foo(xi...) // defer causes foo to run last | |
bar(xi) | |
} | |
func foo(x ...int) { | |
var total int | |
for _, v := range x { | |
total += v | |
} | |
fmt.Println("Printing from foo()", total) | |
} | |
func bar(x []int) { | |
var total int | |
for _, v := range x { | |
total += v | |
} | |
fmt.Println("Printing from bar()", total) | |
} |
This file contains hidden or 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" | |
/* | |
Create a user defined struct with | |
- the identifier “person” | |
- the fields: | |
- first | |
- last | |
- age | |
attach a method to type person with | |
- the identifier “speak” | |
- the method should have the person say their name and age | |
create a value of type person | |
call the method from the value of type person | |
*/ | |
type Person struct { | |
first string | |
last string | |
age int | |
} | |
func (p Person) speak() { | |
fmt.Printf("Hello my name is %v %v and my age is %v.", p.first, p.last, p.age) | |
} | |
func main() { | |
p1 := Person{ | |
first: "Jonathan", | |
last: "Loescher", | |
age: 38, | |
} | |
p1.speak() | |
} |
This file contains hidden or 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" | |
"math" | |
) | |
/* | |
create a type SQUARE | |
create a type CIRCLE | |
attach a method to each that calculates AREA and returns it | |
- circle area= π r 2 | |
- square area = L * W | |
create a type SHAPE that defines an interface as anything that has the AREA method | |
create a func INFO which takes type shape and then prints the area | |
create a value of type square | |
create a value of type circle | |
use func info to print the area of square | |
use func info to print the area of circle | |
*/ | |
type Square struct { | |
length float64 | |
} | |
func (s Square) area() float64 { | |
return s.length * s.length | |
} | |
type Rectangle struct { | |
length, width float64 | |
} | |
func (r Rectangle) area() float64 { | |
return r.width * r.length | |
} | |
type Circle struct { | |
radius float64 | |
} | |
func (c Circle) area() float64 { | |
return math.Pi * (c.radius * c.radius) | |
} | |
type Shape interface { | |
area() float64 | |
} | |
func info(shape Shape) { | |
fmt.Printf("%T: %v\n", shape, shape.area()) | |
} | |
func main() { | |
c1 := Circle{radius: 30} | |
info(c1) | |
s1 := Square{ | |
length: 10, | |
} | |
info(s1) | |
r1 := Rectangle{ | |
length: 10, | |
width: 20, | |
} | |
info(r1) | |
} |
This file contains hidden or 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" | |
// Build and use an anonymous func | |
func main() { | |
func(s string, i int) { | |
fmt.Println(s, i) | |
}("This is an anonymous function.", 38) | |
} |
This file contains hidden or 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" | |
// Assign a func to a variable, then call that func | |
func main() { | |
f := func(age int) { | |
fmt.Println("This is a function assigned to a variable.") | |
fmt.Println("My age is:", age) | |
} | |
f(38) | |
} |
This file contains hidden or 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" | |
"time" | |
) | |
/* | |
Create a func which returns a func | |
assign the returned func to a variable | |
call the returned func | |
*/ | |
func main() { | |
x := myReturnedFunc() | |
age := x(1981) | |
fmt.Println(age) | |
} | |
func myReturnedFunc() func(int) int { | |
return func(yearOfBirth int) int { | |
currentTime := time.Now() | |
return currentTime.Year() - yearOfBirth | |
} | |
} |
This file contains hidden or 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" | |
"time" | |
) | |
/* | |
A “callback” is when we pass a func into a func as an argument. For this exercise, | |
pass a func into a func as an argument | |
*/ | |
func main() { | |
years := []int{1981, 1993} | |
ages := calculateAges(getCurrentYear, years...) | |
fmt.Println(ages) | |
} | |
func calculateAges(currentYear func() int, birthYears ...int) []int { | |
var ages []int | |
for _, v := range birthYears { | |
ages = append(ages, currentYear() - v) | |
} | |
return ages | |
} | |
func getCurrentYear() int { | |
currentTime := time.Now() | |
return currentTime.Year() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment