add: first array examples
This commit is contained in:
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// STORY:
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@ -19,6 +26,7 @@ const (
|
||||
func main() {
|
||||
// ALTERNATIVE:
|
||||
// Use this only when you don't know about the elements beforehand
|
||||
//
|
||||
// var books [yearly]string
|
||||
|
||||
books := [yearly]string{
|
||||
@ -28,30 +36,4 @@ func main() {
|
||||
"Kafka's Revenge 2nd Edition",
|
||||
}
|
||||
fmt.Printf("books : %#v\n", books)
|
||||
|
||||
// var (
|
||||
// wBooks [winter]string
|
||||
// sBooks [summer]string
|
||||
// )
|
||||
|
||||
// wBooks[0] = books[0]
|
||||
|
||||
// for i := range sBooks {
|
||||
// sBooks[i] = books[i+1]
|
||||
// }
|
||||
|
||||
// fmt.Printf("\nwinter : %#v\n", wBooks)
|
||||
// fmt.Printf("\nsummer : %#v\n", sBooks)
|
||||
|
||||
// var published [len(books)]bool
|
||||
|
||||
// published[0] = true
|
||||
// published[len(books)-1] = true
|
||||
|
||||
// fmt.Println("\nPublished Books:")
|
||||
// for i, ok := range published {
|
||||
// if ok {
|
||||
// fmt.Printf("+ %s\n", books[i])
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
31
14-arrays/07-assignment/main.go
Normal file
31
14-arrays/07-assignment/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
prev := [3]string{"Kafka's Revenge", "Stay Golden", "Everythingship"}
|
||||
|
||||
// You can't do this:
|
||||
// books = prev
|
||||
|
||||
var books [4]string
|
||||
|
||||
for i, b := range prev {
|
||||
books[i] += b + " 2nd Ed."
|
||||
}
|
||||
|
||||
// copying arrays using slices
|
||||
// copy(books[:], prev[:])
|
||||
|
||||
books[3] = "Awesomeness"
|
||||
|
||||
fmt.Printf("last year:\n%#v\n", prev)
|
||||
fmt.Printf("\nthis year:\n%#v\n", books)
|
||||
}
|
64
14-arrays/08-multi-dimensional/main.go
Normal file
64
14-arrays/08-multi-dimensional/main.go
Normal file
@ -0,0 +1,64 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// ------------------------------------
|
||||
// #1 - THE BEST WAY
|
||||
// ------------------------------------
|
||||
|
||||
students := [...][3]float64{
|
||||
{5, 6, 1},
|
||||
{9, 8, 4},
|
||||
}
|
||||
|
||||
var avg float64
|
||||
|
||||
for _, grades := range students {
|
||||
for _, grade := range grades {
|
||||
avg += grade
|
||||
}
|
||||
}
|
||||
|
||||
const N = float64(len(students) * len(students[0]))
|
||||
fmt.Printf("Avg Grade: %g\n", avg/N)
|
||||
|
||||
// ------------------------------------
|
||||
// #2 - SO SO WAY
|
||||
// ------------------------------------
|
||||
|
||||
// // You don't need to define the types for the inner arrays
|
||||
// students := [2][3]float64{
|
||||
// [3]float64{5, 6, 1},
|
||||
// [3]float64{9, 8, 4},
|
||||
// }
|
||||
|
||||
// var avg float64
|
||||
|
||||
// avg += students[0][0] + students[0][1] + students[0][2]
|
||||
// avg += students[1][0] + students[1][1] + students[1][2]
|
||||
|
||||
// const N = float64(len(students) * len(students[0]))
|
||||
// fmt.Printf("Avg Grade: %g\n", avg/N)
|
||||
|
||||
// ------------------------------------
|
||||
// #3 - MANUAL WAY
|
||||
// ------------------------------------
|
||||
|
||||
// student1 := [3]float64{5, 6, 1}
|
||||
// student2 := [3]float64{9, 8, 4}
|
||||
|
||||
// var avg float64
|
||||
// avg += student1[0] + student1[1] + student1[2]
|
||||
// avg += student2[0] + student2[1] + student2[2]
|
||||
|
||||
// const N = float64(len(student1) * 2)
|
||||
// fmt.Printf("Avg Grade: %g\n", avg/N)
|
||||
}
|
20
14-arrays/09-keyed-elements/01-unkeyed/main.go
Normal file
20
14-arrays/09-keyed-elements/01-unkeyed/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
rates := [3]float64{
|
||||
0.5,
|
||||
2.5,
|
||||
1.5,
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
}
|
28
14-arrays/09-keyed-elements/02-keyed/main.go
Normal file
28
14-arrays/09-keyed-elements/02-keyed/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
rates := [3]float64{
|
||||
0: 0.5, // index: 0
|
||||
1: 2.5, // index: 1
|
||||
2: 1.5, // index: 2
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
|
||||
// above array literal equals to this:
|
||||
//
|
||||
// rates := [3]float64{
|
||||
// 0.5,
|
||||
// 2.5,
|
||||
// 1.5,
|
||||
// }
|
||||
}
|
28
14-arrays/09-keyed-elements/03-keyed-order/main.go
Normal file
28
14-arrays/09-keyed-elements/03-keyed-order/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
rates := [3]float64{
|
||||
1: 2.5, // index: 1
|
||||
0: 0.5, // index: 0
|
||||
2: 1.5, // index: 2
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
|
||||
// above array literal equals to this:
|
||||
//
|
||||
// rates := [3]float64{
|
||||
// 0.5,
|
||||
// 2.5,
|
||||
// 1.5,
|
||||
// }
|
||||
}
|
28
14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go
Normal file
28
14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go
Normal file
@ -0,0 +1,28 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
rates := [3]float64{
|
||||
// index 0 empty
|
||||
// index 1 empty
|
||||
2: 1.5, // index: 2
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
|
||||
// above array literal equals to this:
|
||||
//
|
||||
// rates := [3]float64{
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 1.5,
|
||||
// }
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// ellipsis (...) below calculates the length of the
|
||||
// array automatically
|
||||
rates := [...]float64{
|
||||
// index 0 empty
|
||||
// index 1 empty
|
||||
// index 2 empty
|
||||
// index 3 empty
|
||||
// index 4 empty
|
||||
5: 1.5, // index: 5
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
|
||||
// above array literal equals to this:
|
||||
//
|
||||
// rates := [6]float64{
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 1.5,
|
||||
// }
|
||||
}
|
34
14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go
Normal file
34
14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
rates := [...]float64{
|
||||
// index 1 to 4 empty
|
||||
|
||||
5: 1.5, // index: 5
|
||||
2.5, // index: 6
|
||||
0: 0.5, // index: 0
|
||||
}
|
||||
|
||||
fmt.Println(rates)
|
||||
|
||||
// above array literal equals to this:
|
||||
//
|
||||
// rates := [7]float64{
|
||||
// 0.5,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 0.,
|
||||
// 1.5,
|
||||
// 2.5,
|
||||
// }
|
||||
}
|
@ -11,20 +11,28 @@ import "fmt"
|
||||
|
||||
// REFACTORED VERSION
|
||||
// It uses well-defined names instead of magic numbers.
|
||||
// Thanks to keyed elements and constants.
|
||||
// Thanks to the keyed elements and constants.
|
||||
|
||||
func main() {
|
||||
const (
|
||||
ETH = iota
|
||||
ETH = 9 - iota
|
||||
WAN
|
||||
ICX
|
||||
// you can add more cryptocurrencies here
|
||||
// watch out the -1 index though!
|
||||
)
|
||||
|
||||
rates := [...]float64{
|
||||
ETH: 25.5,
|
||||
WAN: 120.5,
|
||||
ICX: 20,
|
||||
// you can add more cryptocurrencies here
|
||||
}
|
||||
|
||||
// uses well-defined names (ETH, WAN, ...) - good
|
||||
fmt.Printf("1 BTC is %g ETH\n", rates[ETH])
|
||||
fmt.Printf("1 BTC is %g WAN\n", rates[WAN])
|
||||
fmt.Printf("1 BTC is %g ICX\n", rates[ICX])
|
||||
|
||||
fmt.Printf("%#v\n", rates)
|
||||
}
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
const (
|
||||
Mon Weekday = iota
|
||||
Tue
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// search for books in hipster's love
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,3 +1,10 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
Reference in New Issue
Block a user