go example_textvar_test 源码

  • 2022-07-15
  • 浏览 (768)

golang example_textvar_test 代码

文件路径:/src/flag/example_textvar_test.go

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flag_test

import (
	"flag"
	"fmt"
	"net"
	"os"
)

func ExampleTextVar() {
	fs := flag.NewFlagSet("ExampleTextVar", flag.ContinueOnError)
	fs.SetOutput(os.Stdout)
	var ip net.IP
	fs.TextVar(&ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse")
	fs.Parse([]string{"-ip", "127.0.0.1"})
	fmt.Printf("{ip: %v}\n\n", ip)

	// 256 is not a valid IPv4 component
	ip = nil
	fs.Parse([]string{"-ip", "256.0.0.1"})
	fmt.Printf("{ip: %v}\n\n", ip)

	// Output:
	// {ip: 127.0.0.1}
	//
	// invalid value "256.0.0.1" for flag -ip: invalid IP address: 256.0.0.1
	// Usage of ExampleTextVar:
	//   -ip IP address
	//     	IP address to parse (default 192.168.0.100)
	// {ip: <nil>}
}

相关信息

go 源码目录

相关文章

go example_func_test 源码

go example_test 源码

go example_value_test 源码

go export_test 源码

go flag 源码

go flag_test 源码

0  赞