add: first array examples

This commit is contained in:
Inanc Gumus
2018-11-15 16:37:09 +03:00
parent 5abce6adaf
commit 038a61e195
34 changed files with 448 additions and 28 deletions

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
// STORY: // STORY:

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
import "fmt" import "fmt"
@ -19,6 +26,7 @@ const (
func main() { func main() {
// ALTERNATIVE: // ALTERNATIVE:
// Use this only when you don't know about the elements beforehand // Use this only when you don't know about the elements beforehand
//
// var books [yearly]string // var books [yearly]string
books := [yearly]string{ books := [yearly]string{
@ -28,30 +36,4 @@ func main() {
"Kafka's Revenge 2nd Edition", "Kafka's Revenge 2nd Edition",
} }
fmt.Printf("books : %#v\n", books) 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])
// }
// }
} }

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
import "fmt" import "fmt"

View 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)
}

View 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)
}

View 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)
}

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

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

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

View File

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

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

View File

@ -11,20 +11,28 @@ import "fmt"
// REFACTORED VERSION // REFACTORED VERSION
// It uses well-defined names instead of magic numbers. // It uses well-defined names instead of magic numbers.
// Thanks to keyed elements and constants. // Thanks to the keyed elements and constants.
func main() { func main() {
const ( const (
ETH = iota ETH = 9 - iota
WAN WAN
ICX
// you can add more cryptocurrencies here
// watch out the -1 index though!
) )
rates := [...]float64{ rates := [...]float64{
ETH: 25.5, ETH: 25.5,
WAN: 120.5, WAN: 120.5,
ICX: 20,
// you can add more cryptocurrencies here
} }
// uses well-defined names (ETH, WAN, ...) - good // uses well-defined names (ETH, WAN, ...) - good
fmt.Printf("1 BTC is %g ETH\n", rates[ETH]) 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 WAN\n", rates[WAN])
fmt.Printf("1 BTC is %g ICX\n", rates[ICX])
fmt.Printf("%#v\n", rates)
} }

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import ( import (

View File

@ -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 ( const (
Mon Weekday = iota Mon Weekday = iota
Tue Tue

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import ( import (

View File

@ -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 package main
// --------------------------------------------------------- // ---------------------------------------------------------

View File

@ -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 package main
func main() { func main() {

View File

@ -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 package main
// --------------------------------------------------------- // ---------------------------------------------------------

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
// search for books in hipster's love // search for books in hipster's love

View File

@ -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 package main
import ( import (

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
import "fmt" import "fmt"

View File

@ -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 package main
import "fmt" import "fmt"