当前位置: 首页>>代码示例>>Golang>>正文


Golang sort.StringSlice函数代码示例

本文整理汇总了Golang中sort.StringSlice函数的典型用法代码示例。如果您正苦于以下问题:Golang StringSlice函数的具体用法?Golang StringSlice怎么用?Golang StringSlice使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了StringSlice函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestEnvMap

func TestEnvMap(t *testing.T) {
	tt.StartTest(t)
	defer tt.FinishTest(t)

	expected := []string{
		"START_COMMAND=XYZ",
		"TEST_CROSSREF1=cross_pkg2",
		"TEST_CROSSREF2=cross_pkg1",
		"TEST_CROSSREF_VAL1=cross_pkg1",
		"TEST_CROSSREF_VAL2=cross_pkg2",
		"TEST_ENV_VARIABLE=TEST_ENV_CONTENT",
		"TEST_MERGE_UNDEFINED=undef:",
		"TEST_MERGE_VARIABLE=pkg2:pkg1:",
		"TEST_OVERRIDE_VARIABLE=pkg2",
	}

	root := NewEnvMap()
	root.Set("TEST_ENV_VARIABLE", "TEST_ENV_CONTENT")
	root.Set("TEST_MERGE_VARIABLE", "pkg1:$TEST_MERGE_VARIABLE")
	root.Set("TEST_MERGE_UNDEFINED", "undef:$TEST_NOT_SET")
	root.Set("TEST_OVERRIDE_VARIABLE", "pkg1")
	root.Set("TEST_CROSSREF1", "$TEST_CROSSREF_VAL2")
	root.Set("TEST_CROSSREF_VAL1", "cross_pkg1")
	root.Set("START_COMMAND", "XYZ")

	c1 := root.NewChild()
	c1.Set("TEST_MERGE_VARIABLE", "pkg2:$TEST_MERGE_VARIABLE")
	c1.Set("TEST_OVERRIDE_VARIABLE", "pkg2")
	c1.Set("TEST_CROSSREF2", "$TEST_CROSSREF_VAL1")
	c1.Set("TEST_CROSSREF_VAL2", "cross_pkg2")
	c1.Set("START_COMMAND", "XYZ")

	envstrs := c1.Strings()

	// Sort the two list just in case.
	sort.Sort(sort.StringSlice(envstrs))
	sort.Sort(sort.StringSlice(expected))

	msg := make([]string, 0, len(envstrs))
	failed := false
	a := func(fmtstr string, args ...interface{}) {
		msg = append(msg, fmt.Sprintf(fmtstr, args...))
	}
	for i := 0; i < len(expected) || i < len(envstrs); i++ {
		if i >= len(expected) {
			a("\t'' > '%s'", envstrs[i])
		} else if i >= len(envstrs) {
			a("\t'%s' < ''", expected[i])
		} else if expected[i] != envstrs[i] {
			a("\t'%s' != '%s'", expected[i], envstrs[i])
		} else {
			a("\t'%s' == '%s'", expected[i], envstrs[i])
			continue
		}
		failed = true
	}
	if failed == true {
		tt.Fatalf(t, "results are not the same:\n%s", strings.Join(msg, "\n"))
	}
}
开发者ID:wallyqs,项目名称:util,代码行数:60,代码来源:envmap_test.go

示例2: TestFilter_Keys

func TestFilter_Keys(t *testing.T) {
	valmap := map[string]interface{}{
		"string": "what what",
		"int":    3242,
		"bool":   true,
	}

	actualkeys := make([]string, len(valmap))

	for key, _ := range valmap {
		actualkeys = append(actualkeys, key)
	}

	fil, err := testFilterSuite(valmap)
	if err != nil {
		t.Log(err)
		t.FailNow()
	}

	sortedActual := sort.StringSlice(actualkeys)
	sortedActual.Sort()
	sortedReported := sort.StringSlice(fil.Keys())
	sortedReported.Sort()

	if !reflect.DeepEqual(sortedActual, sortedReported) {
		t.Log("Array keys are not the same!")
	}
}
开发者ID:hfern,项目名称:goseq,代码行数:28,代码来源:filter_test.go

示例3: testSortFn

func testSortFn(t *testing.T, fn sortFn, fnName string) {
	for _, test := range []sort.Interface{
		sort.IntSlice([]int{660, 14, 796, 336, 223, 594, 419, 574, 372, 103, 991, 718, 436, 351, 844, 277, 668, 250, 330, 86}),
		sort.Float64Slice([]float64{213.237, 458.642, 978.311, 547.351, 57.8992, 245.518, 445.638, 251.79, 960.202, 100.069, 483.136, 407.858, 496.913, 562.943, 557.959, 219.648, 164.599, 843.304, 671.732, 222.676}),
		sort.StringSlice([]string{"assaults", "brackish", "monarchism", "ascribe", "mechanize", "andiron", "overpricing", "jading", "hauliers", "snug", "zodiac", "credit", "tremendous", "palavered", "hibiscuses", "amplest", "interrogated", "geologic", "unorthodoxy", "propagated"}),
	} {
		// Make a copy to avoid modification of the original slice.
		var data sort.Interface
		switch v := test.(type) {
		case sort.IntSlice:
			data = append(sort.IntSlice(nil), v...)
		case sort.Float64Slice:
			data = append(sort.Float64Slice(nil), v...)
		case sort.StringSlice:
			data = append(sort.StringSlice(nil), v...)
		default:
			t.Errorf("missing case: cannot copy %T", v)
			continue
		}
		fn(data)
		if !sort.IsSorted(data) {
			t.Errorf("%s(%v)", fnName, test)
			t.Errorf(" got %v", data)
			sort.Sort(data)
			t.Errorf("want %v", data)
		}
	}
}
开发者ID:mrekucci,项目名称:epi,代码行数:28,代码来源:algorithm_test.go

示例4: Marshal

// Marshal parses the response from the aws sdk into an awsm LoadBalancer
func (l *LoadBalancer) Marshal(balancer *elb.LoadBalancerDescription, region string, secGrpList *SecurityGroups, vpcList *Vpcs, subList *Subnets) {

	// security groups
	secGroupNames := secGrpList.GetSecurityGroupNames(aws.StringValueSlice(balancer.SecurityGroups))
	secGroupNamesSorted := sort.StringSlice(secGroupNames[0:])
	secGroupNamesSorted.Sort()

	// subnets
	subnetNames := subList.GetSubnetNames(aws.StringValueSlice(balancer.Subnets))
	subnetNamesSorted := sort.StringSlice(subnetNames[0:])
	subnetNamesSorted.Sort()

	l.Name = aws.StringValue(balancer.LoadBalancerName)
	l.DNSName = aws.StringValue(balancer.DNSName)
	l.CreatedTime = aws.TimeValue(balancer.CreatedTime) // robots
	l.CreatedHuman = humanize.Time(l.CreatedTime)       // humans
	l.VpcID = aws.StringValue(balancer.VPCId)
	l.Vpc = vpcList.GetVpcName(l.VpcID)
	l.SubnetIDs = aws.StringValueSlice(balancer.Subnets)
	l.Subnets = strings.Join(subnetNamesSorted, ", ")
	l.HealthCheckTarget = aws.StringValue(balancer.HealthCheck.Target)
	l.HealthCheckInterval = fmt.Sprintf("%d seconds", *balancer.HealthCheck.Interval)
	l.Scheme = aws.StringValue(balancer.Scheme)
	l.SecurityGroups = strings.Join(secGroupNamesSorted, ", ")
	l.AvailabilityZones = strings.Join(aws.StringValueSlice(balancer.AvailabilityZones), ", ") // TODO
	l.Region = region
}
开发者ID:murdinc,项目名称:awsm,代码行数:28,代码来源:loadbalancers.go

示例5: Sort

func Sort(buf []string) {
	if *reverse {
		sort.Sort(sort.Reverse(sort.StringSlice(buf)))
	} else {
		sort.Sort(sort.StringSlice(buf))
	}
}
开发者ID:rkoesters,项目名称:goblin,代码行数:7,代码来源:sort.go

示例6: main

func main() {
	// sort people as type
	studyGroup := people{"Zeno", "John", "Al", "Jenny"}

	fmt.Println(studyGroup)
	sort.Sort(studyGroup)
	fmt.Println(studyGroup)
	sort.Sort(sort.Reverse(studyGroup))
	fmt.Println(studyGroup)

	// sort s as string slice
	s := []string{"Zeno", "John", "Al", "Jenny"}
	fmt.Println(s)
	sort.StringSlice(s).Sort()
	fmt.Println(s)
	sort.Sort(sort.Reverse(sort.StringSlice(s)))
	fmt.Println(s)

	// sort n as int slice
	n := []int{7, 4, 8, 2, 9, 19, 12, 32, 3}
	fmt.Println(n)
	sort.IntSlice(n).Sort()
	fmt.Println(n)
	sort.Sort(sort.Reverse(sort.IntSlice(n)))
	fmt.Println(n)
}
开发者ID:plod,项目名称:goLearning,代码行数:26,代码来源:main.go

示例7: TestWikiTypicalCRUD

func TestWikiTypicalCRUD(t *testing.T) {
	session := createSession(t)
	defer session.Close()

	w := WikiTest{session, t}
	w.CreateSchema()
	for _, page := range wikiTestData {
		if err := w.InsertPage(page); err != nil {
			t.Error("InsertPage:", err)
		}
	}
	if count := w.GetPageCount(); count != len(wikiTestData) {
		t.Errorf("count: expected %d, got %d\n", len(wikiTestData), count)
	}
	for _, original := range wikiTestData {
		page := new(WikiPage)
		if err := w.SelectPage(page, original.Title, original.RevId); err != nil {
			t.Error("SelectPage:", err)
			continue
		}
		sort.Sort(sort.StringSlice(page.Tags))
		sort.Sort(sort.StringSlice(original.Tags))
		if !reflect.DeepEqual(page, original) {
			t.Errorf("page: expected %#v, got %#v\n", original, page)
		}
	}
}
开发者ID:dankennedy,项目名称:gocql,代码行数:27,代码来源:wiki_test.go

示例8: Equals

// Equals returns whether the contents of two sets are identical
func (us *unsafeSet) Equals(other Set) bool {
	v1 := sort.StringSlice(us.Values())
	v2 := sort.StringSlice(other.Values())
	v1.Sort()
	v2.Sort()
	return reflect.DeepEqual(v1, v2)
}
开发者ID:jonboulle,项目名称:fleet,代码行数:8,代码来源:set.go

示例9: equal

// Check that two slices contents are equal; order is irrelevant
func equal(a, b []string) bool {
	as := sort.StringSlice(a)
	bs := sort.StringSlice(b)
	as.Sort()
	bs.Sort()
	return reflect.DeepEqual(as, bs)
}
开发者ID:Blystad,项目名称:deis,代码行数:8,代码来源:set_test.go

示例10: Sync

func (c *httpClusterClient) Sync(ctx context.Context) error {
	mAPI := NewMembersAPI(c)
	ms, err := mAPI.List(ctx)
	if err != nil {
		return err
	}

	c.Lock()
	defer c.Unlock()

	eps := make([]string, 0)
	for _, m := range ms {
		eps = append(eps, m.ClientURLs...)
	}
	sort.Sort(sort.StringSlice(eps))

	ceps := make([]string, len(c.endpoints))
	for i, cep := range c.endpoints {
		ceps[i] = cep.String()
	}
	sort.Sort(sort.StringSlice(ceps))
	// fast path if no change happens
	// this helps client to pin the endpoint when no cluster change
	if reflect.DeepEqual(eps, ceps) {
		return nil
	}

	return c.SetEndpoints(eps)
}
开发者ID:jak-atx,项目名称:vic,代码行数:29,代码来源:client.go

示例11: TestMapToSlice

func TestMapToSlice(t *testing.T) {
	type expect struct {
		keysToExclude []string
		payload       map[string]string
		result        []string
	}

	expectations := []expect{
		expect{[]string{"key2", "key3"}, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{"key1", "value1"}},
		expect{[]string{"key1"}, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{"key2", "value2", "key3", "value3"}},
		expect{[]string{"key2", "not-exist"}, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{"key1", "value1", "key3", "value3"}},
		expect{[]string{"key1", "key2", "key3"}, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{}},
		expect{[]string{}, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{"key1", "value1", "key2", "value2", "key3", "value3"}},
		expect{nil, map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, []string{"key1", "value1", "key2", "value2", "key3", "value3"}},
	}

	for _, e := range expectations {
		result := mapToSlice(e.keysToExclude, e.payload)
		eres := sort.StringSlice(e.result)
		res := sort.StringSlice(result)

		eres.Sort()
		res.Sort()
		assert.EqualValues(t, eres, res, "Result doesn't match the expectation")
	}
}
开发者ID:ifraixedes,项目名称:go-hmac-validator,代码行数:26,代码来源:validator_test.go

示例12: computeUpdatedSCC

// computeUpdatedSCC determines if the expected SCC looks like the actual SCC
// it does this by making the expected SCC mirror the actual SCC for items that
// we are not reconciling and performing a diff (ignoring changes to metadata).
// If a diff is produced then the expected SCC is submitted as needing an update.
func (o *ReconcileSCCOptions) computeUpdatedSCC(expected kapi.SecurityContextConstraints, actual kapi.SecurityContextConstraints) (*kapi.SecurityContextConstraints, bool) {
	needsUpdate := false

	// if unioning old and new groups/users then make the expected contain all
	// also preserve and set priorities
	if o.Union {
		groupSet := sets.NewString(actual.Groups...)
		groupSet.Insert(expected.Groups...)
		expected.Groups = groupSet.List()

		userSet := sets.NewString(actual.Users...)
		userSet.Insert(expected.Users...)
		expected.Users = userSet.List()

		if actual.Priority != nil {
			expected.Priority = actual.Priority
		}
	}

	// sort users and groups to remove any variants in order when diffing
	sort.StringSlice(actual.Groups).Sort()
	sort.StringSlice(actual.Users).Sort()
	sort.StringSlice(expected.Groups).Sort()
	sort.StringSlice(expected.Users).Sort()

	// make a copy of the expected scc here so we can ignore metadata diffs.
	updated := expected
	expected.ObjectMeta = actual.ObjectMeta

	if !kapi.Semantic.DeepEqual(expected, actual) {
		needsUpdate = true
	}

	return &updated, needsUpdate
}
开发者ID:johnmccawley,项目名称:origin,代码行数:39,代码来源:reconcile_sccs.go

示例13: matchArrs

func (m *messagesToEmitMatcher) matchArrs(actual, expected []routing_table.RegistryMessage) bool {
	if len(actual) != len(expected) {
		return false
	}

	fixedActual := []routing_table.RegistryMessage{}
	fixedExpected := []routing_table.RegistryMessage{}

	for _, message := range actual {
		sort.Sort(sort.StringSlice(message.URIs))
		fixedActual = append(fixedActual, message)
	}

	for _, message := range expected {
		sort.Sort(sort.StringSlice(message.URIs))
		fixedExpected = append(fixedExpected, message)
	}

	sort.Sort(ByMessage(fixedActual))
	sort.Sort(ByMessage(fixedExpected))

	for i := 0; i < len(fixedActual); i++ {
		if !reflect.DeepEqual(fixedActual[i], fixedExpected[i]) {
			return false
		}
	}

	return true
}
开发者ID:emc-xchallenge,项目名称:route-emitter,代码行数:29,代码来源:emit_messages_matcher.go

示例14: main

func main() {

	studyGroup := people{"Zeno", "Ian", "John", "Al", "Jenny"}

	fmt.Println("1: = sorting slice of strings of type people by name:")
	fmt.Println("\to:", studyGroup)
	sort.Strings(studyGroup)
	fmt.Println("\t\t [Strings method]:", studyGroup)
	sort.Sort(sort.StringSlice(studyGroup))
	fmt.Println("\t\t [StringSlice interface conversion]", studyGroup)
	sort.Sort(sort.Reverse(sort.StringSlice(studyGroup)))
	fmt.Println("\t\t [Reverse]", studyGroup)

	fmt.Println("2: = by literal slice of strings")
	s := []string{"Zeno", "John", "Al", "Jenny", "Ben"}
	fmt.Println("\to:", s)
	sort.Strings(s)
	fmt.Println("\t\t [Strings method]:", s)
	sort.Sort(sort.StringSlice(s))
	fmt.Println("\t\t [StringSlice interface conversion]", s)
	sort.Sort(sort.Reverse(sort.StringSlice(s)))
	fmt.Println("\t\t [Reverse]", s)

	fmt.Println("3: = slice of ints")
	n := []int{7, 4, 8, 2, 9, 19, 12, 32, 3}
	fmt.Println("\to:", n)
	sort.Ints(n)
	fmt.Println("\t\t [Ints method]", n)
	sort.Sort(sort.IntSlice(n))
	fmt.Println("\t\t [IntSlice interface conversion]", n)
	sort.Sort(sort.Reverse(sort.IntSlice(n)))
	fmt.Println("\t\t [Reverse]", n)

}
开发者ID:imcmurray,项目名称:UdemyTraining,代码行数:34,代码来源:main.go

示例15: testPeerListsMatch

func testPeerListsMatch(t *testing.T, p1, p2 []peer.ID) {

	if len(p1) != len(p2) {
		t.Fatal("did not find as many peers as should have", p1, p2)
	}

	ids1 := make([]string, len(p1))
	ids2 := make([]string, len(p2))

	for i, p := range p1 {
		ids1[i] = string(p)
	}

	for i, p := range p2 {
		ids2[i] = string(p)
	}

	sort.Sort(sort.StringSlice(ids1))
	sort.Sort(sort.StringSlice(ids2))

	for i := range ids1 {
		if ids1[i] != ids2[i] {
			t.Fatal("Didnt find expected peer", ids1[i], ids2)
		}
	}
}
开发者ID:djbarber,项目名称:ipfs-hack,代码行数:26,代码来源:dht_test.go


注:本文中的sort.StringSlice函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。