本文整理汇总了Golang中sort.Strings函数的典型用法代码示例。如果您正苦于以下问题:Golang Strings函数的具体用法?Golang Strings怎么用?Golang Strings使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Strings函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sortQuery
func sortQuery(u *url.URL) {
q := u.Query()
if len(q) == 0 {
return
}
arKeys := make([]string, len(q))
i := 0
for k, _ := range q {
arKeys[i] = k
i++
}
sort.Strings(arKeys)
buf := new(bytes.Buffer)
for _, k := range arKeys {
sort.Strings(q[k])
for _, v := range q[k] {
if buf.Len() > 0 {
buf.WriteRune('&')
}
buf.WriteString(fmt.Sprintf("%s=%s", k, url.QueryEscape(v)))
}
}
// Rebuild the raw query string
u.RawQuery = buf.String()
}
示例2: filterTermsByMode
func (s *SortField) filterTermsByMode(terms []string) string {
if len(terms) == 1 || (len(terms) > 1 && s.Mode == SortFieldDefault) {
return terms[0]
} else if len(terms) > 1 {
switch s.Mode {
case SortFieldMin:
sort.Strings(terms)
return terms[0]
case SortFieldMax:
sort.Strings(terms)
return terms[len(terms)-1]
}
}
// handle missing terms
if s.Missing == SortFieldMissingLast {
if s.Desc {
return LowTerm
}
return HighTerm
}
if s.Desc {
return HighTerm
}
return LowTerm
}
示例3: Equal
// Equal checks the equality of two resource configs.
func (c *ResourceConfig) Equal(c2 *ResourceConfig) bool {
// If either are nil, then they're only equal if they're both nil
if c == nil || c2 == nil {
return c == c2
}
// Sort the computed keys so they're deterministic
sort.Strings(c.ComputedKeys)
sort.Strings(c2.ComputedKeys)
// Two resource configs if their exported properties are equal.
// We don't compare "raw" because it is never used again after
// initialization and for all intents and purposes they are equal
// if the exported properties are equal.
check := [][2]interface{}{
{c.ComputedKeys, c2.ComputedKeys},
{c.Raw, c2.Raw},
{c.Config, c2.Config},
}
for _, pair := range check {
if !reflect.DeepEqual(pair[0], pair[1]) {
return false
}
}
return true
}
示例4: UnitStates
func (f *FakeRegistry) UnitStates() ([]*unit.UnitState, error) {
f.Lock()
defer f.Unlock()
states := make([]*unit.UnitState, 0)
// Sort by unit name, then by machineID
sUnitNames := make([]string, 0)
for name := range f.jobStates {
sUnitNames = append(sUnitNames, name)
}
sort.Strings(sUnitNames)
for _, name := range sUnitNames {
sMIDs := make([]string, 0)
for machineID := range f.jobStates[name] {
sMIDs = append(sMIDs, machineID)
}
sort.Strings(sMIDs)
for _, mID := range sMIDs {
states = append(states, f.jobStates[name][mID])
}
}
return states, nil
}
示例5: modulesStr
func modulesStr(ms []*Module) string {
result := ""
order := make([]int, 0, len(ms))
ks := make([]string, 0, len(ms))
mapping := make(map[string]int)
for i, m := range ms {
k := m.Id()
ks = append(ks, k)
mapping[k] = i
}
sort.Strings(ks)
for _, k := range ks {
order = append(order, mapping[k])
}
for _, i := range order {
m := ms[i]
result += fmt.Sprintf("%s\n", m.Id())
ks := make([]string, 0, len(m.RawConfig.Raw))
for k, _ := range m.RawConfig.Raw {
ks = append(ks, k)
}
sort.Strings(ks)
result += fmt.Sprintf(" source = %s\n", m.Source)
for _, k := range ks {
result += fmt.Sprintf(" %s\n", k)
}
}
return strings.TrimSpace(result)
}
示例6: String
func (r Request) String() string {
queryString := ""
if len(r.QueryParams) > 0 {
keys := make([]string, 0, len(r.QueryParams))
queryParts := make([]string, 0, len(r.QueryParams))
for k := range r.QueryParams {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
for _, val := range r.QueryParams[k] {
queryParts = append(queryParts, fmt.Sprintf("%s=%s", k, url.QueryEscape(val)))
}
}
queryString = "?" + strings.Join(queryParts, "&")
}
var headers []string
if len(r.Headers) > 0 {
var headerKeys []string
for k := range r.Headers {
headerKeys = append(headerKeys, k)
}
sort.Strings(headerKeys)
for _, k := range headerKeys {
for _, val := range r.Headers[k] {
headers = append(headers, fmt.Sprintf("%s:%s", k, val))
}
}
}
return fmt.Sprintf("%s %s%s\n%s\n%s", r.Method, r.Route, queryString, headers, r.Body)
}
示例7: Error
func (e *BadInterfacesError) Error() string {
inverted := make(map[string][]string)
for name, reason := range e.issues {
inverted[reason] = append(inverted[reason], name)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "snap %q has bad plugs or slots: ", e.snap)
reasons := make([]string, 0, len(inverted))
for reason := range inverted {
reasons = append(reasons, reason)
}
sort.Strings(reasons)
for _, reason := range reasons {
names := inverted[reason]
sort.Strings(names)
for i, name := range names {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(name)
}
fmt.Fprintf(&buf, " (%s); ", reason)
}
return strings.TrimSuffix(buf.String(), "; ")
}
示例8: Test_IterKeys
func Test_IterKeys(t *testing.T) {
loop := 100
expectedKeys := make([]string, loop)
m := New()
for i := 0; i < loop; i++ {
key := strconv.Itoa(i)
expectedKeys[i] = key
m.Set(key, i)
}
keys := make([]string, 0)
for key := range m.IterKeys() {
keys = append(keys, key)
}
if len(keys) != len(expectedKeys) {
t.Error("IterKeys doesn't loop the right times")
}
sort.Strings(keys)
sort.Strings(expectedKeys)
for i, v := range keys {
if v != expectedKeys[i] {
t.Error("IterKeys doesn't loop over the right keys")
}
}
}
示例9: assertDirAtPath
func assertDirAtPath(root *Directory, pth string, children []string) error {
fsn, err := DirLookup(root, pth)
if err != nil {
return err
}
dir, ok := fsn.(*Directory)
if !ok {
return fmt.Errorf("%s was not a directory", pth)
}
listing, err := dir.List()
if err != nil {
return err
}
var names []string
for _, d := range listing {
names = append(names, d.Name)
}
sort.Strings(children)
sort.Strings(names)
if !compStrArrs(children, names) {
return errors.New("directories children did not match!")
}
return nil
}
示例10: TestColumnsQuoted
func TestColumnsQuoted(t *testing.T) {
once.Do(setup)
p := new(Person)
names, err := ColumnsQuoted(p, true)
if err != nil {
t.Errorf("Error getting ColumnsQuoted: %v", err)
}
lst := []string{"id", "name", "Email", "Age", "opened", "closed", "updated", "height"}
sort.Strings(lst)
for i, orig := range lst {
lst[i] = Default.quoted(orig)
}
expected := strings.Join(lst, ",")
if len(names) != len(expected) {
t.Errorf("Length mismatch: expected %d, got %d", len(expected), len(names))
}
fields := strings.Split(names, ",")
sort.Strings(fields)
names = strings.Join(fields, ",")
if expected != names {
t.Errorf("Mismatch: expected %s, got %s", expected, names)
}
}
示例11: sameIMMembers
func sameIMMembers(a *isMasterResponse, b *isMasterResponse) bool {
if a == nil && b == nil {
return true
}
if a == nil {
a = &emptyIsMasterResponse
}
if b == nil {
b = &emptyIsMasterResponse
}
l := len(a.Hosts)
if l != len(b.Hosts) {
return false
}
aHosts := make([]string, 0, l+1)
bHosts := make([]string, 0, l+1)
for i := 0; i < l; i++ {
aHosts = append(aHosts, a.Hosts[i])
bHosts = append(bHosts, b.Hosts[i])
}
sort.Strings(aHosts)
sort.Strings(bHosts)
aHosts = append(aHosts, a.Primary)
bHosts = append(bHosts, b.Primary)
for i := range aHosts {
if aHosts[i] != bHosts[i] {
return false
}
}
return true
}
示例12: sameRSMembers
func sameRSMembers(a *replSetGetStatusResponse, b *replSetGetStatusResponse) bool {
if (a == nil || len(a.Members) == 0) && (b == nil || len(b.Members) == 0) {
return true
}
if a == nil || b == nil {
return false
}
l := len(a.Members)
if l != len(b.Members) {
return false
}
aMembers := make([]string, 0, l)
bMembers := make([]string, 0, l)
for i := 0; i < l; i++ {
aM := a.Members[i]
aMembers = append(aMembers, fmt.Sprintf("%s:%s", aM.Name, aM.State))
bM := b.Members[i]
bMembers = append(bMembers, fmt.Sprintf("%s:%s", bM.Name, bM.State))
}
sort.Strings(aMembers)
sort.Strings(bMembers)
for i := 0; i < l; i++ {
if aMembers[i] != bMembers[i] {
return false
}
}
return true
}
示例13: GetTypeInfo
// 获取类型的类型信息
func GetTypeInfo(typ reflect.Type) TypeInfo {
realType := FromPtrType(typ)
switch realType.Kind() {
case reflect.Struct:
typeMemberNameList := make([]string, realType.NumField())
typeMemberInfoList := make([]TypeMemberInfo, realType.NumField())
for i := 0; i < len(typeMemberNameList); i++ {
typeMemberNameList[i] = realType.Field(i).Name
}
sort.Strings(typeMemberNameList)
for i, typeMemberName := range typeMemberNameList {
typeMember, _ := realType.FieldByName(typeMemberName)
typeMemberInfoList[i] = TypeMemberInfo{TypeMemberName: typeMember.Name, TypeMemberTypeName: typeMember.Type.String()}
}
return TypeInfo{TypeName: realType.Name(), TypeMemberInfos: typeMemberInfoList}
case reflect.Interface:
typeMemberNameList := make([]string, realType.NumMethod())
typeMemberInfoList := make([]TypeMemberInfo, realType.NumMethod())
for i := 0; i < len(typeMemberNameList); i++ {
typeMemberNameList[i] = realType.Method(i).Name
}
sort.Strings(typeMemberNameList)
for i, typeMemberName := range typeMemberNameList {
typeMember, _ := realType.MethodByName(typeMemberName)
typeMemberInfoList[i] = TypeMemberInfo{TypeMemberName: typeMember.Name, TypeMemberTypeName: typeMember.Type.String()}
}
return TypeInfo{TypeName: realType.Name(), TypeMemberInfos: typeMemberInfoList}
default:
return TypeInfo{TypeName: realType.Name()}
}
}
示例14: search
func search(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
year, _ := strconv.Atoi(query.Get("year"))
actor := query.Get("actor")
genre := query.Get("genre")
page, _ := strconv.Atoi(query.Get("page"))
if page == 0 {
page = 1
}
director := query.Get("director")
movies, count, err := searchMovies(page, year, actor, genre, director)
if err != nil {
log.Println("searchMovies ", 1, err)
}
genres := []string{}
years := []string{}
for _, movie := range movies {
year := strconv.Itoa(movie.Year)
if stringInSlice(years, year) == false {
years = append(years, year)
}
filmGenres := movie.Genre
for _, genre := range filmGenres {
if stringInSlice(genres, genre) == false {
genres = append(genres, genre)
}
}
}
sort.Strings(genres)
sort.Strings(years)
cont := context{Title: "NNM-club RSS", Static: staticURL, Movies: movies, Genres: genres, Years: years, Count: count}
render(w, cont, "index")
}
示例15: TestExplainTrace
func TestExplainTrace(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sqlDB, _ := setup(t)
defer cleanup(s, sqlDB)
if _, err := sqlDB.Exec(`CREATE DATABASE test; CREATE TABLE test.foo (id INT PRIMARY KEY)`); err != nil {
t.Fatal(err)
}
rows, err := sqlDB.Query(`EXPLAIN (TRACE) INSERT INTO test.foo VALUES (1)`)
if err != nil {
t.Fatal(err)
}
expParts := []string{"coordinator", "node 1"}
var parts []string
pretty := rowsToStrings(rows)
for _, row := range pretty[1:] {
part := row[3] // Operation
if ind := sort.SearchStrings(parts, part); ind == len(parts) || parts[ind] != part {
parts = append(parts, part)
sort.Strings(parts)
}
}
sort.Strings(expParts)
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expParts, parts) {
t.Fatalf("expected %v, got %v\n\nResults:\n%v", expParts, parts, prettyPrint(pretty))
}
}