我正在掌握Golang的做事方式.对于可能能够提供以下帮助的人,我非常感激不尽.首先是一些示例代码
package main
import (
"log"
"os"
)
func logIt(s string) {
f, _ := os.OpenFile("errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND,
0666)
defer f.Close()
log.SetOutput(f)
log.Println(s)
}
type iAm func(string)
func a(iam string) { logIt(iam + " A") }
func b(iam string) { logIt(iam + " B") }
func c(iam string) { logIt(iam + " C") }
var funcs = map[string]iAm{"A": a, "B": b, "C": c}
func main() {
funcs["A"]("Je suis")
funcs["B"]("Ich bin")
funcs["A"]("Yo soy")
funcs["D"]("Soy Yo")
}
Run Code Online (Sandbox Code Playgroud)
说明