GO中的Flag包和指针相关

方式一

package main

import (
	"flag"
	"fmt"
)

func main() {
	var  name =new(string)// [1]
	flag.StringVar(name, "name", "everyone", "The greeting object.") // [2]

	
	flag.Parse()
	fmt.Printf("Hello, %v!\n", *name)

}

版本二

package main

import (
	"flag"
	"fmt"
)

func main() {
	var  name string// [1]
	flag.StringVar(&name, "name", "everyone", "The greeting object.") // [2]

	flag.Parse()
	fmt.Printf("Hello, %v!\n", name)

}

方式三

package main

import (
	"flag"
	"fmt"
)

func main() {
	var  name *string// [1]
	flag.StringVar(name, "name", "everyone", "The greeting object.") // [2]

	flag.Parse()
	fmt.Printf("Hello, %v!\n", name)

}

报错

posted on 2021-06-09 16:23  yangras  阅读(32)  评论(0编辑  收藏  举报

导航