add: arrays

This commit is contained in:
Inanc Gumus
2018-11-17 21:56:09 +03:00
parent c0dbce8062
commit a3a0d39a0b
138 changed files with 2022 additions and 1905 deletions

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

@ -0,0 +1,21 @@
// 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{
25.5, // ethereum
120.5, // wanchain
}
// uses magic values - not good
fmt.Printf("1 BTC is %g ETH\n", rates[0])
fmt.Printf("1 BTC is %g WAN\n", rates[1])
}

View File

@ -0,0 +1,38 @@
// 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"
// REFACTORED VERSION
// It uses well-defined names instead of magic numbers.
// Thanks to the keyed elements and constants.
func main() {
const (
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)
}