本文整理汇总了Golang中strings.Map函数的典型用法代码示例。如果您正苦于以下问题:Golang Map函数的具体用法?Golang Map怎么用?Golang Map使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Map函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUnmarshal
func TestUnmarshal(t *testing.T) {
for i, tt := range unmarshalTests {
var scan Scanner
in := []byte(tt.in)
if err := checkValid(in, &scan); err != nil {
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: checkValid: %#v", i, err)
continue
}
}
if tt.ptr == nil {
continue
}
// v = new(right-type)
v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec := NewDecoder(bytes.NewReader(in))
if tt.useNumber {
dec.UseNumber()
}
if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: %v, want %v", i, err, tt.err)
continue
} else if err != nil {
continue
}
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out)
data, _ := Marshal(v.Elem().Interface())
println(string(data))
data, _ = Marshal(tt.out)
println(string(data))
continue
}
// Check round trip.
if tt.err == nil {
enc, err := Marshal(v.Interface())
if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err)
continue
}
vv := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec = NewDecoder(bytes.NewReader(enc))
if tt.useNumber {
dec.UseNumber()
}
if err := dec.Decode(vv.Interface()); err != nil {
t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err)
continue
}
if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface())
t.Errorf(" In: %q", strings.Map(noSpace, string(in)))
t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc)))
continue
}
}
}
}
示例2: splitList
// splitList splits a comma-separated list into a slice of strings, accounting
// for escape characters.
func splitList(source string) (results []string) {
var (
isEscaped, hasEscape bool
lastIndex, index int
)
for ; index < len(source); index++ {
if isEscaped {
isEscaped = false
continue
}
switch source[index] {
case '\\':
isEscaped = true
hasEscape = true
case ',':
result := source[lastIndex:index]
if hasEscape {
result = strings.Map(removeEscape, result)
hasEscape = false
}
results = append(results, result)
lastIndex = index + 1
}
}
if lastIndex < index {
result := source[lastIndex:]
if hasEscape {
result = strings.Map(removeEscape, result)
}
results = append(results, result)
}
return results
}
示例3: FindExtension
func (desc *FileDescriptorSet) FindExtension(packageName string, typeName string, fieldName string) (extPackageName string, field *FieldDescriptorProto) {
parent := desc.GetMessage(packageName, typeName)
if parent == nil {
return "", nil
}
if !parent.IsExtendable() {
return "", nil
}
extendee := "." + packageName + "." + typeName
for _, file := range desc.GetFile() {
for _, ext := range file.GetExtension() {
if strings.Map(dotToUnderscore, file.GetPackage()) == strings.Map(dotToUnderscore, packageName) {
if !(ext.GetExtendee() == typeName || ext.GetExtendee() == extendee) {
continue
}
} else {
if ext.GetExtendee() != extendee {
continue
}
}
if ext.GetName() == fieldName {
return file.GetPackage(), ext
}
}
}
return "", nil
}
示例4: gccgoSymbolPrefix
// Return the package prefix when using gccgo.
func (p *Package) gccgoSymbolPrefix() string {
if !*gccgo {
return ""
}
clean := func(r rune) rune {
switch {
case 'A' <= r && r <= 'Z', 'a' <= r && r <= 'z',
'0' <= r && r <= '9':
return r
}
return '_'
}
if *gccgopkgpath != "" {
return strings.Map(clean, *gccgopkgpath)
}
if *gccgoprefix == "" && p.PackageName == "main" {
return "main"
}
prefix := strings.Map(clean, *gccgoprefix)
if prefix == "" {
prefix = "go"
}
return prefix + "." + p.PackageName
}
示例5: ShouldContainModuloWhiteSpace
func ShouldContainModuloWhiteSpace(haystack interface{}, expectedNeedle ...interface{}) string {
if fail := need(1, expectedNeedle); fail != success {
return fail
}
value, valueIsString := haystack.(string)
expecNeedle, expecIsString := expectedNeedle[0].(string)
if !valueIsString || !expecIsString {
return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(haystack), reflect.TypeOf(expectedNeedle[0]))
}
elimWs := func(r rune) rune {
if r == ' ' || r == '\t' || r == '\n' {
return -1 // drop the rune
}
return r
}
h := strings.Map(elimWs, value)
n := strings.Map(elimWs, expecNeedle)
if strings.Contains(h, n) {
return success
}
return fmt.Sprintf(shouldContainModuloWS, value, expecNeedle)
}
示例6: getFieldNumber
func getFieldNumber(descriptorSet *descriptor.FileDescriptorSet, rootPkg string, rootMsg string, msg *descriptor.DescriptorProto, fieldNum int32) *descriptor.FieldDescriptorProto {
for _, f := range msg.GetField() {
if f.GetNumber() == fieldNum {
return f
}
}
if !msg.IsExtendable() {
return nil
}
extendee := "." + rootPkg + "." + rootMsg
for _, file := range descriptorSet.GetFile() {
for _, ext := range file.GetExtension() {
if strings.Map(dotToUnderscore, file.GetPackage()) == strings.Map(dotToUnderscore, rootPkg) {
if !(ext.GetExtendee() == rootMsg || ext.GetExtendee() == extendee) {
continue
}
} else {
if ext.GetExtendee() != extendee {
continue
}
}
if ext.GetNumber() == fieldNum {
return ext
}
}
}
return nil
}
示例7: String
func (n Name) String() (s string) {
s = strings.TrimLeft(string(n), "0123456789")
valid := func(r rune) rune {
if r >= '0' && r <= '9' {
return r
}
if r >= 'a' && r <= 'z' {
return r
}
if r >= 'A' && r <= 'Z' {
return r
}
if r == '_' || r == '-' {
return '_'
}
return -1
}
s = strings.Map(valid, s)
if len(s) == 0 {
return "_" + strings.Map(valid, string(n))
}
return strings.Title(s)
}
示例8: TestFault
func TestFault(t *testing.T) {
f := &Fault{Code: 4, Message: "Too many parameters."}
buf := bytes.NewBuffer(nil)
err := Marshal(buf, "", f)
if err != nil {
t.Fatal(fmt.Sprintf("error marshalling Fault: %s", err))
}
t.Logf("marshalled fault: %s", buf.Bytes())
repl := func(r rune) rune {
switch r {
case ' ', '\t', '\n', '\r':
return -1
}
return r
}
f1S := strings.Map(repl, buf.String())
f2S := strings.Map(repl, xmlFault)
if f1S != f2S {
t.Errorf("fatal != constant\n%s\n!=\n%s", f1S, f2S)
}
_, _, f2, err := Unmarshal(bytes.NewBuffer(buf.Bytes()))
if err != nil {
t.Fatalf("cannot unmarshal previously marshalled fault (\n%s\n):%s",
buf, err)
}
if f2.String() != f.String() {
t.Errorf("f1=%s != f2=%s", f, f2)
}
}
示例9: SignPolicy
// SignPolicy return the proper signature and other parameters needed to
// generate a valid Cloudfront Signed URL.
// For canned policies this is: Expires, Signature, Key-Pair-Id
// For custom policies this is: Policy, Signature, Key-Pair-Id
//
// More information:
// http://goo.gl/pvA97e
// Command line equivalent:
// cat policy | openssl sha1 -sign cloudfront-keypair.pem | openssl base64 | tr '+=/' '-_~'
func SignPolicy(privateKey PrivateKey, policy PolicySigner, keyPairID string) (string, error) {
signature, err := policy.signWithPrivateKey(privateKey)
if err != nil {
return "", fmt.Errorf("Cannot sign policy: %v", err)
}
encoding := base64.NewEncoding(encodeCloudFront)
paddingMap := func(r rune) rune {
switch r {
case '=':
return '_'
default:
return r
}
}
switch policy.(type) {
case CannedPolicy:
return fmt.Sprintf("Expires=%d&Signature=%s&Key-Pair-Id=%s",
policy.(CannedPolicy).ExpiresAt.Unix(),
strings.Map(paddingMap, encoding.EncodeToString(signature)),
keyPairID,
), nil
case CustomPolicy:
return fmt.Sprintf("Policy=%s&Signature=%s&Key-Pair-Id=%s",
strings.Map(paddingMap, encoding.EncodeToString([]byte(policy.String()))),
strings.Map(paddingMap, encoding.EncodeToString(signature)),
keyPairID,
), nil
}
return "", nil
}
示例10: TestLetterOnlyMapFunction
func (l *LibSuite) TestLetterOnlyMapFunction(c *C) {
var input string = "ABC123"
var output string = strings.Map(letterOnlyMapF, input)
c.Assert(output, Equals, "ABC")
input = "abc123"
output = strings.Map(letterOnlyMapF, input)
c.Assert(output, Equals, "ABC")
}
示例11: ToUpper
// ToUpper takes the given string and upper cases it based on the
// current CASEMAPPING setting given by the server.
func (s *State) ToUpper(name string) string {
switch *s.ISupport("CASEMAPPING") {
case "ascii":
return strings.Map(ASCIIToUpper, name)
case "strict-rfc1459":
return strings.Map(StrictRFC1459ToUpper, name)
}
return strings.Map(RFC1459ToUpper, name)
}
示例12: compare
func compare(solution, output string) string {
switch {
case solution == output:
return "Accepted"
case strings.Map(keepNum, solution) == strings.Map(keepNum, output):
return "Presentation Error"
default:
return "Wrong Answer"
}
}
示例13: getCoordsFromCellIDString
// getCoordsFromCellIDString returns the zero based cartesian
// coordinates from a cell name in Excel format, e.g. the cellIDString
// "A1" returns 0, 0 and the "B3" return 1, 2.
func getCoordsFromCellIDString(cellIDString string) (x, y int, error error) {
var letterPart string = strings.Map(letterOnlyMapF, cellIDString)
y, error = strconv.Atoi(strings.Map(intOnlyMapF, cellIDString))
if error != nil {
return x, y, error
}
y -= 1 // Zero based
x = lettersToNumeric(letterPart)
return x, y, error
}
示例14: invokeFunc
func invokeFunc(vs, rs string, ports []gdc.APIPort, fn portAction) []error {
n := 0
e := []error{}
for _, binding := range ports {
if binding.PrivatePort == 0 || binding.PublicPort == 0 {
// There is a bug in GDC where unexported ports will have PrivatePort
// set to zero, instead of PublicPort. So checking both, just in case.
continue
}
if binding.IP == "0.0.0.0" {
// Rewrite "catch-all" host to a real host's IP address.
binding.IP = hostIPs[0].String()
}
// Mangle the VS name.
vsID := fmt.Sprintf("%s-%d-%s", strings.Map(func(r rune) rune {
switch r {
case '/', ':':
return '-'
default:
return r
}
}, vs), binding.PrivatePort, binding.Type)
// Mangle the RS name.
rsID := fmt.Sprintf("%s-%d-%s", strings.Map(func(r rune) rune {
switch r {
case '/', ':':
return '-'
default:
return r
}
}, rs), binding.PublicPort, binding.Type)
// There must be leading slash in the swarm event stream for node names that needs
// to be trimmed now that it has been munged
rsID = strings.Trim(rsID, "-")
if err := fn(vsID, rsID, binding); err == nil {
n++
} else {
e = append(e, err)
}
}
if n == 0 {
log.Warnf("no public ports were processed for [%s]", path.Join(vs, rs))
}
return e
}
示例15: TestLetterOnlyMapFunction
func TestLetterOnlyMapFunction(t *testing.T) {
var input string = "ABC123"
var output string = strings.Map(letterOnlyMapF, input)
if output != "ABC" {
t.Error("Expected output == 'ABC' but got ", output)
}
input = "abc123"
output = strings.Map(letterOnlyMapF, input)
if output != "ABC" {
t.Error("Expected output == 'ABC' but got ", output)
}
}