reorganize: starting with funcs
This commit is contained in:
26
25-functions/01-basics/bad.go
Normal file
26
25-functions/01-basics/bad.go
Normal file
@ -0,0 +1,26 @@
|
||||
// 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 showN() {
|
||||
if N == 0 {
|
||||
fmt.Println("showN : N is zero, increment it.")
|
||||
return
|
||||
}
|
||||
fmt.Printf("showN : N = %d\n", N)
|
||||
}
|
||||
|
||||
func incrN() {
|
||||
N++
|
||||
}
|
||||
|
||||
// you cannot declare a function within the same package with the same name
|
||||
// func incrN() {
|
||||
// }
|
33
25-functions/01-basics/main.go
Normal file
33
25-functions/01-basics/main.go
Normal file
@ -0,0 +1,33 @@
|
||||
// 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/
|
||||
//
|
||||
|
||||
//
|
||||
// You need run this program like so:
|
||||
// go run .
|
||||
//
|
||||
// This will magically pass all the go files in the current directory to the
|
||||
// Go compiler.
|
||||
//
|
||||
//
|
||||
// BUT NOT like so:
|
||||
// go run main.go
|
||||
//
|
||||
// Because, the compiler needs to see global.go too
|
||||
// It can't magically find global.go — what you give is what you get.
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// N is a shared counter which is BAD
|
||||
var N int
|
||||
|
||||
func main() {
|
||||
showN()
|
||||
incrN()
|
||||
incrN()
|
||||
showN()
|
||||
}
|
Reference in New Issue
Block a user