Skip to content

Instantly share code, notes, and snippets.

@nna774
Created Nov 28, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@nna774 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