Go language uses a string of several tips to share _golang

Source: Internet
Author: User

One, the bottom of the string is a byte array

This is really important, and it affects some of the other techniques below. When you create a string, its essence is an array of bytes. This means that you can access a single byte just as you would access an array. For example, the following code prints each byte in the string and each byte in the corresponding byte array individually:

Package main

Import "FMT"

func Main () { 
  str: = "Hello" for
  I: = 0; i < len (str); i++ {
    fmt. Printf ("%b%s\n", Str[i], string (str[i))
  }
}

This is a very important knowledge, so there is a second technique ...

Using a byte array or buffer can increase the speed of string concatenation

The string is read-only in the go language, which means that each time you use it str = str + "something" , a new string object is actually created. If you seek the highest efficiency of your code, you should use a byte buffer instead, for example:

Package main

Import ( 
  "bytes"
  "FMT"
)

func main () { 
  str: = "Something"
  buf: = bytes. Newbufferstring (str) for
  I: = 0; i < 1000; i++ {
    buf. Write ([]byte (RandomString ()))
  }
  fmt. Println (BUF. String ())
}

func randomstring () string { 
  ret: = "Pretend-this-is-random" return
  ret
}

Using a byte array further increases the efficiency of the above code, but you need to know the size of the final string. An intuitive example is the implementation in the Go language left-pad .

Third, you can splice strings like other arrays

When you need to intercept a part of a string, you can manipulate it like a part of an array, sample code:

Package main

Import "FMT"

func Main () { 
  str: = "Xbodycontentx"
  content: = Str[1:len (str)-1]
  Fmt. Println (content)
}

Use ' symbols to create multiple lines of string

This is fairly simple, and you want to define a string in your code that contains multiple lines of address information, then you need to use ' this character as follows:

Package main

Import "FMT"

func Main () { 
  str: = ' Mr Smith
123 something St 
Some city, CA 94043 ' 
  FM T.println (str)
}

Five, you can embed Unicode characters in a string

Assuming that WebSocket communication is implemented, you need to allow the transmitted data to start with 0x00 and end with 0xFF [source code]

We can do this in any string with the following code:

Package main

Import "FMT"

func Main () { 
  str: = "\x00bodycontent\xff"
  fmt. Println (str)
}

Similarly, you can use a Unicode string to handle it, or you can use the original character in a string. For example, the following code is valid:

Package main

Import "FMT"

func Main () { 
  A: = "ÿay!"
  B: = "\u00ffay!"
  Fmt. Println (A, b)
}

Summarize

The technique of using a string on the go language has been shared here, has everyone learned? Learned that this will be helpful for everyone to use the go language, if you have any questions you can exchange messages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.