當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Value.Set方法代碼示例

本文整理匯總了Golang中flag.Value.Set方法的典型用法代碼示例。如果您正苦於以下問題:Golang Value.Set方法的具體用法?Golang Value.Set怎麽用?Golang Value.Set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flag.Value的用法示例。


在下文中一共展示了Value.Set方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: setFromEnv

func setFromEnv(into flag.Value, envVars string) bool {
	multiValued, isMulti := into.(multiValued)

	if len(envVars) > 0 {
		for _, rev := range strings.Split(envVars, " ") {
			ev := strings.TrimSpace(rev)
			if len(ev) == 0 {
				continue
			}

			v := os.Getenv(ev)
			if len(v) == 0 {
				continue
			}
			if !isMulti {
				if err := into.Set(v); err == nil {
					return true
				}
				continue
			}

			vs := strings.Split(v, ",")
			if err := setMultivalued(multiValued, vs); err == nil {
				return true
			}
		}
	}
	return false
}
開發者ID:jault3,項目名稱:mow.cli,代碼行數:29,代碼來源:utils.go

示例2: TestIPNet

func TestIPNet(t *testing.T) {
	testCases := []struct {
		input    string
		success  bool
		expected string
	}{
		{"0.0.0.0/0", true, "0.0.0.0/0"},
		{" 0.0.0.0/0 ", true, "0.0.0.0/0"},
		{"1.2.3.4/8", true, "1.0.0.0/8"},
		{"127.0.0.1/16", true, "127.0.0.0/16"},
		{"255.255.255.255/19", true, "255.255.224.0/19"},
		{"255.255.255.255/32", true, "255.255.255.255/32"},
		{"", false, ""},
		{"/0", false, ""},
		{"0", false, ""},
		{"0/0", false, ""},
		{"localhost/0", false, ""},
		{"0.0.0/4", false, ""},
		{"0.0.0./8", false, ""},
		{"0.0.0.0./12", false, ""},
		{"0.0.0.256/16", false, ""},
		{"0.0.0.0 /20", false, ""},
		{"0.0.0.0/ 24", false, ""},
		{"0 . 0 . 0 . 0 / 28", false, ""},
		{"0.0.0.0/33", false, ""},
	}

	for i := range testCases {
		tc := &testCases[i]
		var f flag.Value = &IPNet{}
		err := f.Set(tc.input)
		if err != nil && tc.success == true {
			t.Errorf("expected success, got %q", err)
			continue
		} else if err == nil && tc.success == false {
			t.Errorf("expected failure")
			continue
		} else if tc.success {
			if f.String() != tc.expected {
				t.Errorf("expected %q, got %q", tc.expected, f.String())
			}
		}
	}
}
開發者ID:ericcapricorn,項目名稱:kubernetes,代碼行數:44,代碼來源:net_test.go

示例3: TestIP

func TestIP(t *testing.T) {
	testCases := []struct {
		input    string
		success  bool
		expected string
	}{
		{"0.0.0.0", true, "0.0.0.0"},
		{" 0.0.0.0 ", true, "0.0.0.0"},
		{"1.2.3.4", true, "1.2.3.4"},
		{"127.0.0.1", true, "127.0.0.1"},
		{"255.255.255.255", true, "255.255.255.255"},
		{"", false, ""},
		{"0", false, ""},
		{"localhost", false, ""},
		{"0.0.0", false, ""},
		{"0.0.0.", false, ""},
		{"0.0.0.0.", false, ""},
		{"0.0.0.256", false, ""},
		{"0 . 0 . 0 . 0", false, ""},
	}

	for i := range testCases {
		tc := &testCases[i]
		var f flag.Value = &IP{}
		err := f.Set(tc.input)
		if err != nil && tc.success == true {
			t.Errorf("expected success, got %q", err)
			continue
		} else if err == nil && tc.success == false {
			t.Errorf("expected failure")
			continue
		} else if tc.success {
			if f.String() != tc.expected {
				t.Errorf("expected %q, got %q", tc.expected, f.String())
			}
		}
	}
}
開發者ID:ericcapricorn,項目名稱:kubernetes,代碼行數:38,代碼來源:net_test.go

示例4: main

func main() {
	var flag flag.Value = new(MyFlag)
	flag.Set("joe")
}
開發者ID:zouyx,項目名稱:learnGo,代碼行數:4,代碼來源:8.4.2.go


注:本文中的flag.Value.Set方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。