File: type_assert.go

package info (click to toggle)
golang-github-docopt-docopt-go 0.6.2%2Bgit20180111.ee0de3b-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 10
file content (31 lines) | stat: -rw-r--r-- 576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
)

func main() {
	usage := `usage: type_assert [-x] [-y]`

	arguments, err := docopt.ParseDoc(usage)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(arguments)

	var x = arguments["-x"].(bool) // type assertion required
	if x == true {
		fmt.Println("x is true")
	}

	y := arguments["-y"] // no type assertion needed
	if y == true {
		fmt.Println("y is true")
	}
	y2 := arguments["-y"]
	if y2 == 10 { // this will never be true, a type assertion would have produced a build error
		fmt.Println("y is 10")
	}
}