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


Golang StringSlice.Sort方法代码示例

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


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

示例1: Usage

func (cmdr *Commander) Usage() {
	fmt.Printf("usage: %s cmd [cmd-flags] [cmd-args]\n", os.Args[0])

	var groupNames sort.StringSlice
	cmdNamesByGroup := map[string]sort.StringSlice{}
	for _, cmd := range cmdr.Commands {
		if _, ok := cmdNamesByGroup[cmd.Group]; !ok {
			groupNames = append(groupNames, cmd.Group)
		}

		cmdNamesByGroup[cmd.Group] = append(cmdNamesByGroup[cmd.Group], cmd.Name)
	}

	groupNames.Sort()

	for _, gn := range groupNames {
		fmt.Printf("\n%s:\n", gn)

		cmdNamesByGroup[gn].Sort()

		for _, cn := range cmdNamesByGroup[gn] {
			cmd := cmdr.Commands[cn]
			fmt.Printf("    %-18s %s\n", cmd.Name, cmd.Description)
		}
	}

	fmt.Println()
}
开发者ID:xsleonard,项目名称:cmd,代码行数:28,代码来源:cmd.go

示例2: ConfigList

func ConfigList(configStore *config.Store, app, env string) error {

	cfg, err := configStore.GetApp(app, env)
	if err != nil {
		return err
	}

	if cfg == nil {
		return fmt.Errorf("unable to list config for %s.", app)
	}

	keys := sort.StringSlice{"ENV"}
	for k, _ := range cfg.Env() {
		keys = append(keys, k)
	}

	keys.Sort()

	for _, k := range keys {
		if k == "ENV" {
			log.Printf("%s=%s\n", k, env)
			continue
		}
		fmt.Printf("%s=%s\n", k, cfg.Env()[k])
	}

	return nil
}
开发者ID:sguzwf,项目名称:galaxy,代码行数:28,代码来源:config.go

示例3: setUsage

func setUsage(commands map[string]Command) {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr,
			"Usage:\n  %s [http://cbfs:8484/] cmd [-opts] cmdargs\n",
			os.Args[0])

		fmt.Fprintf(os.Stderr, "\nCommands:\n")

		ss := sort.StringSlice{}
		for k := range commands {
			ss = append(ss, k)
		}
		ss.Sort()

		for _, k := range ss {
			fmt.Fprintf(os.Stderr, "  %s %s\n", k, commands[k].Argstr)
		}

		fmt.Fprintf(os.Stderr, "\n---- Subcommand Options ----\n")

		for _, k := range ss {
			if commands[k].Flags != nil {
				fmt.Fprintf(os.Stderr, "\n%s:\n", k)
				commands[k].Flags.PrintDefaults()
			}
		}

		os.Exit(1)
	}
}
开发者ID:piaohai,项目名称:cbfs,代码行数:30,代码来源:tool.go

示例4: setUpdate

func setUpdate(prev []string, v interface{}) []string {
	m := map[string]bool{}
	for _, v := range prev {
		m[v] = true
	}

	switch i := v.(type) {
	case nil:
	case string:
		m[i] = true
	case []interface{}:
		for _, v := range i {
			val, ok := v.(string)
			if ok {
				m[val] = true
			}
		}
	}

	rv := sort.StringSlice{}
	for k := range m {
		rv = append(rv, k)
	}
	rv.Sort()
	return []string(rv)
}
开发者ID:couchbaselabs,项目名称:statstore,代码行数:26,代码来源:data.go

示例5: init

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:\n%s [opts] template\n\nOptions:\n",
			os.Args[0])
		flag.PrintDefaults()
		tdoc := map[string]string{
			"{{.ID}}":          "numeric ID of repo",
			"{{.Owner.Login}}": "github username of repo owner",
			"{{.Owner.ID}}":    "github numeric id of repo owner",
			"{{.Name}}":        "short name of repo (e.g. gitmirror)",
			"{{.FullName}}":    "full name of repo (e.g. dustin/gitmirror)",
			"{{.Language}}":    "repository language (if detected)",
		}

		a := sort.StringSlice{}
		for k := range tdoc {
			a = append(a, k)
		}
		a.Sort()

		fmt.Fprintf(os.Stderr, "\nTemplate parameters:\n")
		tw := tabwriter.NewWriter(os.Stderr, 8, 4, 2, ' ', 0)
		for _, k := range a {
			fmt.Fprintf(tw, "  %v\t- %v\n", k, tdoc[k])
		}
		tw.Flush()
		fmt.Fprintf(os.Stderr, "\nExample templates:\n"+
			"  http://example.com/gitmirror/{{.FullName}}.git\n"+
			"  http://example.com/gitmirror/{{.Owner.Login}}/{{.Language}}/{{.Name}}.git\n"+
			"  http://example.com/gitmirror/{{.Name}}.git\n")
	}
}
开发者ID:kohenkatz,项目名称:gitmirror,代码行数:32,代码来源:setuphooks.go

示例6: ComputeChecksum

// computeChecksum computes checksum of all servers in the ring
func (r *hashRing) ComputeChecksum() {
	var addresses sort.StringSlice
	var buffer bytes.Buffer

	r.servers.Lock()
	for address := range r.servers.byAddress {
		addresses = append(addresses, address)
	}

	addresses.Sort()

	for _, address := range addresses {
		buffer.WriteString(address)
		buffer.WriteString(";")
	}

	old := r.servers.checksum
	r.servers.checksum = farm.Fingerprint32(buffer.Bytes())
	r.ringpop.ringEvent(RingChecksumEvent{
		OldChecksum: old,
		NewChecksum: r.servers.checksum,
	})

	r.servers.Unlock()
}
开发者ID:coderQQ,项目名称:ringpop-go,代码行数:26,代码来源:hashring.go

示例7: _CheckSignature

// 校验消息是否是从微信服务器发送过来的.
// 使用 buf 能提高一点性能 和 减少一些对 GC 的压力, buf 的长度最好 >=128
func _CheckSignature(signature, timestamp, nonce, token string, buf []byte) bool {
	const hashsumLen = 40 // sha1

	if len(signature) != hashsumLen {
		return false
	}

	bufLen := hashsumLen + len(timestamp) + len(nonce) + len(token)
	if len(buf) < bufLen {
		buf = make([]byte, hashsumLen, bufLen)
	} else {
		buf = buf[:hashsumLen]
	}

	strArray := sort.StringSlice{token, timestamp, nonce}
	strArray.Sort()

	buf = append(buf, strArray[0]...)
	buf = append(buf, strArray[1]...)
	buf = append(buf, strArray[2]...)

	hashsumArray := sha1.Sum(buf[hashsumLen:]) // require go1.2+

	hashsumHexBytes := buf[:hashsumLen]
	hex.Encode(hashsumHexBytes, hashsumArray[:])

	// 采用 subtle.ConstantTimeCompare 是防止 计时攻击!
	if rslt := subtle.ConstantTimeCompare(hashsumHexBytes, []byte(signature)); rslt == 1 {
		return true
	}
	return false
}
开发者ID:GOCMS,项目名称:wechat,代码行数:34,代码来源:signature.go

示例8: display

func display(w io.Writer, format string, title string, supportedSites map[string]string) {

	var urls sort.StringSlice
	for url := range supportedSites {
		urls = append(urls, url)
	}
	urls.Sort()

	switch format {
	case "html":
		fmt.Fprint(w, "\t\tSupported "+title+":")
		fmt.Fprint(w, "<br />\n")
		for _, url := range urls {
			var s string

			if strings.Contains(url, "http") {
				s = "\t\t<a href=\"" + url + "\">" + supportedSites[url] + "</a><br />\n"
			} else {
				s = "\t\t<a href=\"http://www." + url + "\">" + supportedSites[url] + "</a><br />\n"
			}

			fmt.Fprint(w, s)
		}
	default:
		fmt.Fprint(w, "Supported journals:\n\n")
		for _, url := range urls {
			fmt.Fprint(w, supportedSites[url]+"\t\t"+url+"\n")
		}
	}
}
开发者ID:frankMilde,项目名称:rol,代码行数:30,代码来源:store.go

示例9: findFile

// findFile finds a file that ends with name.md, ignoring leading digits and dashes (to support blogging and
// sorting of .md files a-là jekyll) and returns it's name. It returns an error if no path is found.
// Names ending with / are automatically added index to them. If there are multiple paths ending with name.md,
// they are sorted lexicographically and the biggest path is returned, so e.g. 2014-01-01-results.md
// will have preference over 2013-01-01-results.md
func findFile(root, name string) (string, error) {
	if strings.HasSuffix(name, "/") || name == "" {
		name += "index"
	}
	name += ".md"
	basename := filepath.Base(name)
	dirname := filepath.Dir(name)
	matches, err := filepath.Glob(filepath.Join(root, dirname, "*"+basename))
	if err != nil {
		return "", err
	}
	if len(matches) == 0 {
		return "", fmt.Errorf("no matches for pattern %s", name)
	}
	var prefixes sort.StringSlice
	for _, m := range matches {
		m = filepath.Base(m)
		if p := m[:len(m)-len(basename)]; isDigital(p) {
			prefixes = append(prefixes, p)
		}
	}
	if len(prefixes) == 0 {
		return "", fmt.Errorf("no matches for pattern %s", name)
	}
	prefixes.Sort()
	return filepath.Join(root, dirname, prefixes[len(prefixes)-1]+basename), nil
}
开发者ID:grafikdb,项目名称:grafik-public,代码行数:32,代码来源:handlers.go

示例10: UploadDirTree

// UploadDirTree uploads a local directory and all of its subfolders
// localDir 		-> path to the local folder to upload along with all of its subfolders.
// remoteRootDir 	-> the root folder on the FTP server where to store the localDir tree.
// excludedDirs		-> a slice of folder names to exclude from the uploaded directory tree.
// callback			-> a callback function, which is called synchronously. Do remember to collect data in a go routine for instance if you do not want the upload to block.
// Returns the number of files uploaded and an error if any.
//
// The current workding directory is set back to the initial value at the end.
func (ftp *FTP) UploadDirTree(localDir string, remoteRootDir string, maxSimultaneousConns int, excludedDirs []string, callback Callback) (n int, err error) {

	if len(remoteRootDir) == 0 {
		return n, errors.New("A valid remote root folder with write permission needs specifying.")
	}

	var pwd string
	if pwd, err = ftp.Pwd(); err != nil {
		return
	}

	if _, err = ftp.Cwd(remoteRootDir); err != nil {
		return n, nil
	}
	//go back to original wd
	defer ftp.Cwd(pwd)

	//all lower case
	var exDirs sort.StringSlice
	if len(excludedDirs) > 0 {
		exDirs = sort.StringSlice(excludedDirs)
		for _, v := range exDirs {
			v = strings.ToLower(v)
		}
		exDirs.Sort()
	}

	err = ftp.uploadDirTree(localDir, exDirs, callback, &n)
	if err != nil {
		ftp.writeInfo(fmt.Sprintf("An error while uploading the folder %s occurred.", localDir))
	}

	return n, err
}
开发者ID:ghp268,项目名称:ftp4go-1,代码行数:42,代码来源:clientutil.go

示例11: String

func (ee EtcdEnvironment) String() (out string) {
	norm := normalizeSvcEnv(ee)

	if val, ok := norm["DISCOVERY_URL"]; ok {
		delete(norm, "DISCOVERY_URL")
		if _, ok := norm["DISCOVERY"]; !ok {
			norm["DISCOVERY"] = val
		}
	}

	var sorted sort.StringSlice
	for k, _ := range norm {
		sorted = append(sorted, k)
	}
	sorted.Sort()

	out += "[Service]\n"

	for _, key := range sorted {
		val := norm[key]
		out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val)
	}

	return
}
开发者ID:rcleere,项目名称:coreos-cloudinit,代码行数:25,代码来源:etcd.go

示例12: AminoAcidCode

// Returns the code for an ambigious amino acid.
func AminoAcidCode(aa string) string {
	aa = strings.ToUpper(aa)
	if len(aa) == 1 {
		return aa
	}
	letters := sort.StringSlice{aa[:]}
	letters.Sort()
	sorted := strings.Join([]string(letters), "")
	switch sorted {
	case "AG":
		return "R"
	case "CT":
		return "Y"
	case "AC":
		return "M"
	case "GT":
		return "K"
	case "CG":
		return "S"
	case "AT":
		return "W"
	case "ACT":
		return "H"
	case "CGT":
		return "B"
	case "ACG":
		return "V"
	case "AGT":
		return "D"
	case "ACGT":
		return "N"
	}
	return aa
}
开发者ID:baruchlubinsky,项目名称:bioutil,代码行数:35,代码来源:amino_acids.go

示例13: sortFlags

// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
	var list sort.StringSlice
	for _, f := range flags {
		if len(f.Names) == 1 {
			list = append(list, f.Names[0])
			continue
		}

		found := false
		fName := strings.TrimPrefix(strings.TrimPrefix(f.Names[0], "#"), "-")
		for _, name := range list {
			if name == fName {
				found = true
				break
			}
		}
		if !found {
			list = append(list, fName)
		}
	}
	list.Sort()
	result := make([]*Flag, len(list))
	for i, name := range list {
		result[i] = flags[name]
	}
	return result
}
开发者ID:rjjb3427,项目名称:docker,代码行数:28,代码来源:flag.go

示例14: generalHelp

func generalHelp() {
	out := tabwriter.NewWriter(os.Stderr, 0, 8, 1, '\t', 0)
	exe := filepath.Base(os.Args[0])
	fmt.Fprintf(out, "USAGE:\n\t%s [global options] <command> [command options] [arguments...]\n\n", exe)
	fmt.Fprintf(out, "VERSION:\n\t%s\n\n", Version)

	fmt.Fprintf(out, "COMMANDS:\n")
	names := sort.StringSlice{}
	for name, _ := range subcommands {
		names = append(names, name)
	}
	names.Sort()
	for _, name := range names {
		cmd := subcommands[name]
		fmt.Fprintf(out, "\t%s\t%s\n", name, cmd.desc())
	}
	fmt.Fprintln(out)

	fmt.Fprintf(out, "GLOBAL OPTIONS:\n")
	flag.VisitAll(func(f *flag.Flag) {
		prefix := "-"
		if len(f.Name) > 1 {
			prefix = "--"
		}
		fmt.Fprintf(out, "\t%s%s=%s\t%s\n", prefix, f.Name, f.DefValue, f.Usage)
	})

	fmt.Fprintln(out)
	fmt.Fprintf(out, "Run \"%s help <command>\" for more details about a command.\n", exe)
}
开发者ID:logan,项目名称:heim,代码行数:30,代码来源:subcommands.go

示例15: String

func (l list) String() string {
	var lines sort.StringSlice
	for k, v := range l.entry {
		lines = append(lines, fmt.Sprintf("%q:%v,\n", k, v))
	}
	lines.Sort()
	return strings.Join(lines, "")
}
开发者ID:britannic,项目名称:blacklist,代码行数:8,代码来源:list.go


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