本文整理汇总了Golang中sort.StringSlice类的典型用法代码示例。如果您正苦于以下问题:Golang StringSlice类的具体用法?Golang StringSlice怎么用?Golang StringSlice使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringSlice类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: 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)
}
}
示例3: 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
}
示例4: 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
}
示例5: 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
}
示例6: 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
}
示例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
}
示例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")
}
}
}
示例9: 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()
}
示例10: 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()
}
示例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
}
示例12: 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
}
示例13: Less
func (d destinations) Less(i, j int) bool {
switch d[i].routeFamily {
case bgp.RF_FS_IPv4_UC, bgp.RF_FS_IPv6_UC, bgp.RF_FS_IPv4_VPN, bgp.RF_FS_IPv6_VPN, bgp.RF_FS_L2_VPN:
var s, t *bgp.FlowSpecNLRI
switch d[i].routeFamily {
case bgp.RF_FS_IPv4_UC:
s = &d[i].nlri.(*bgp.FlowSpecIPv4Unicast).FlowSpecNLRI
t = &d[j].nlri.(*bgp.FlowSpecIPv4Unicast).FlowSpecNLRI
case bgp.RF_FS_IPv6_UC:
s = &d[i].nlri.(*bgp.FlowSpecIPv6Unicast).FlowSpecNLRI
t = &d[j].nlri.(*bgp.FlowSpecIPv6Unicast).FlowSpecNLRI
case bgp.RF_FS_IPv4_VPN:
s = &d[i].nlri.(*bgp.FlowSpecIPv4VPN).FlowSpecNLRI
t = &d[j].nlri.(*bgp.FlowSpecIPv4VPN).FlowSpecNLRI
case bgp.RF_FS_IPv6_VPN:
s = &d[i].nlri.(*bgp.FlowSpecIPv6VPN).FlowSpecNLRI
t = &d[j].nlri.(*bgp.FlowSpecIPv6VPN).FlowSpecNLRI
case bgp.RF_FS_L2_VPN:
s = &d[i].nlri.(*bgp.FlowSpecL2VPN).FlowSpecNLRI
t = &d[j].nlri.(*bgp.FlowSpecL2VPN).FlowSpecNLRI
}
if r, _ := bgp.CompareFlowSpecNLRI(s, t); r >= 0 {
return true
} else {
return false
}
default:
strings := sort.StringSlice{d[i].nlri.String(), d[j].nlri.String()}
return strings.Less(0, 1)
}
}
示例14: SortedStringHas
func SortedStringHas(s sort.StringSlice, x string) bool {
index := s.Search(x)
if index == len(s) {
return false
}
return s[s.Search(x)] == x
}
示例15: 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)
}