Skip to content

Instantly share code, notes, and snippets.

@pchw
Last active August 29, 2015 14:06
Show Gist options
  • Save pchw/95402ccc35c23a2745bc to your computer and use it in GitHub Desktop.
Save pchw/95402ccc35c23a2745bc to your computer and use it in GitHub Desktop.
ternary failed
package main
import (
"fmt"
)
func main() {
var foo = map[bool]string{true: "foo", false: "bar"}[true]
fmt.Println("Hello Go", foo)
var bar = map[bool]string{true: "foo", false: "bar"}[false]
fmt.Println("Hello Go", bar)
}
/*
Hello Go foo
Hello Go bar
Program exited.
*/
package main
import (
"fmt"
"os"
)
func sendCapture() string {
fmt.Println("Send Capture")
return "url"
}
func sendFile(filename string) string {
fmt.Println("Send File", filename)
return "url"
}
func main() {
var url = map[bool]string{true: sendCapture(), false: sendFile(os.Args[1])}[len(os.Args) == 1]
fmt.Println("Hello Go", url)
}
/*
1
Send Capture
panic: runtime error: index out of range
goroutine 16 [running]:
runtime.panic(0x113be0, 0x1a39dc)
/tmp/sandbox/go/src/pkg/runtime/panic.c:279 +0x120
main.main()
/tmpfs/gosandbox-992bfd34_3434ac75_ca326031_a55b9406_b94753c2/prog.go:20 +0x3a0
goroutine 19 [runnable]:
runfinq()
/tmp/sandbox/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
/tmp/sandbox/go/src/pkg/runtime/proc.c:1445
[process exited with non-zero status]
Program exited.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment