Skip to content

Instantly share code, notes, and snippets.

@nna774
Created November 28, 2020 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nna774/c2839eca89512918496643c2f615e63d to your computer and use it in GitHub Desktop.
Save nna774/c2839eca89512918496643c2f615e63d to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"strconv"
)
type myVar struct {
V int64
SET bool // setされた もsetなのむずかしい。
}
func (m *myVar) String() string {
return "piyopoyo"
}
func (m *myVar) Set(s string) error {
m.SET = true
v, err := strconv.ParseInt(s, 0, strconv.IntSize)
if err != nil {
return err
}
m.V = v
return nil
}
func main() {
mv := myVar{}
flag.Var(&mv, "a", "piyo")
flag.Parse()
fmt.Printf("set? %v\n", mv.SET)
fmt.Printf("%v\n", mv.V)
}
@nna774
Copy link
Author

nna774 commented Nov 28, 2020

$ ./tmp  
set? false
0
$ ./tmp  -a 42
set? true
42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment