add: unfinished code. people are having problems
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
// 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() {
|
||||
ages := [3]int{15, 30, 25}
|
||||
|
||||
for _, age := range ages {
|
||||
fmt.Println(age)
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
// 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() {
|
||||
// uncomment the code below to observe the error
|
||||
|
||||
// ages := [3]int{15, 30, 25, 44}
|
||||
|
||||
// for _, age := range ages {
|
||||
// fmt.Println(age)
|
||||
// }
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
// 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() {
|
||||
// you cannot use variables
|
||||
// when setting the length of an array
|
||||
|
||||
// length := 3
|
||||
|
||||
// but, you can use constants and constant expressions
|
||||
const length = 3 * 2
|
||||
|
||||
ages := [3 * 2]int{15, 30, 25}
|
||||
|
||||
for _, age := range ages {
|
||||
fmt.Println(age)
|
||||
}
|
||||
}
|
||||
|
||||
// EXERCISE:
|
||||
// Try to put the `length` constant
|
||||
// in place of `3 * 2` above.
|
||||
|
||||
// EXERCISE:
|
||||
// Try to put the `length` variable
|
||||
// in place of `3 * 2` above.
|
||||
//
|
||||
// And observe the error.
|
||||
//
|
||||
// To do that, you need comment-out
|
||||
// the length constant first.
|
@@ -0,0 +1,51 @@
|
||||
// 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() {
|
||||
// -----
|
||||
// you cannot use incompatible values
|
||||
// than the array's element type
|
||||
//
|
||||
// uncomment the code parts below one by one
|
||||
// then observe the errors
|
||||
// -----
|
||||
|
||||
// for example you can't use a string
|
||||
// ages := [3]int{15, 30, "hi"}
|
||||
|
||||
// -----
|
||||
|
||||
// or a float64, etc...
|
||||
// ages := [3]int{15, 30, 5.5}
|
||||
|
||||
// -----
|
||||
|
||||
// of course this is valid, since it's untyped
|
||||
// 5. becomes 5 (int)
|
||||
// ages := [3]int{15, 30, 5.}
|
||||
|
||||
// -----
|
||||
|
||||
// for _, age := range ages {
|
||||
// fmt.Println(age)
|
||||
// }
|
||||
}
|
||||
|
||||
// EXERCISE:
|
||||
// Try to put the `length` constant
|
||||
// in place of `3 * 2` above.
|
||||
|
||||
// EXERCISE:
|
||||
// Try to put the `length` variable
|
||||
// in place of `3 * 2` above.
|
||||
//
|
||||
// And observe the error.
|
||||
//
|
||||
// To do that, you need comment-out
|
||||
// the length constant first.
|
@@ -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() {
|
||||
ages := [...]int{15, 30, 25}
|
||||
// equals to:
|
||||
// ages := [3]int{15, 30, 25}
|
||||
|
||||
for _, age := range ages {
|
||||
fmt.Println(age)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user