本文整理汇总了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"))
}
}
示例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!")
}
}
示例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)
}
}
}
示例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
}
示例5: Sort
func Sort(buf []string) {
if *reverse {
sort.Sort(sort.Reverse(sort.StringSlice(buf)))
} else {
sort.Sort(sort.StringSlice(buf))
}
}
示例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)
}
示例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)
}
}
}
示例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)
}
示例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)
}
示例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)
}
示例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")
}
}
示例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
}
示例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
}
示例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)
}
示例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)
}
}
}