add: arrays
This commit is contained in:
20
14-arrays/11-keyed-elements/01-unkeyed/main.go
Normal file
20
14-arrays/11-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/11-keyed-elements/02-keyed/main.go
Normal file
28
14-arrays/11-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/11-keyed-elements/03-keyed-order/main.go
Normal file
28
14-arrays/11-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/11-keyed-elements/04-keyed-auto-initialize/main.go
Normal file
28
14-arrays/11-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/11-keyed-elements/06-keyed-and-unkeyed/main.go
Normal file
34
14-arrays/11-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,
|
||||
// }
|
||||
}
|
@ -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])
|
||||
}
|
@ -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)
|
||||
}
|
Reference in New Issue
Block a user