Learning Golang Language (4): Type--string

Source: Internet
Author: User
Tags learn golang
This is a creation in Article, where the information may have evolved or changed.

Follow the "code technique" to learn Golang language, lazy for many days, today continue to explain the type of Golang. Today, the string is explained.

Learning Golang Language (1): Hello World

Learning Golang Language (2): Variables

Learning Golang Language (3): Type--Boolean and numeric types

Learning Golang Language (4): Type--string

Learning Golang Language (5): Type--array

Learning Golang Language (6): Type--slice


Numeric type

Add a place not indicated in the previous section:

Golang also defines the types of three dependent systems : int, unint, and UIntPtr. Because the number of bits used to represent these types is different on 32-bit systems and 64-bit systems.


String

Another important built-in type in Golang is string.

A string is a sequence of characters concatenated with fixed-length characters. The string of Go is connected by a single byte. That is, the traditional string is made up of characters, and go's string is different, it is composed of bytes.

The byte of the Go language uses UTF-8 encoding to identify Unicode text.
The representation of a string is simple, and is created using double quotation marks ("") or anti-quotation marks ("'). For example: "Hello World" or ' Hello World '.

The difference between the two:

The escape character between the double quotes is escaped, and the characters between the anti-quotes remain unchanged. For example:

Output:

If single quotation marks (') are used to denote one character (UTF-8 encoding)----This is not a string in the go language.

Once the string has been assigned, the string cannot be modified: The string is immutable in go. Some operations are illegal on go.

var s = "Hello World"
S[0] = ' C ' <-----Modify the first character to ' C ', which returns the wrong one.

This needs to be done in go:

1. Convert s to rune array, 2, modify the first element of the array, 3, create a new string s2;4, print output.


Multi-line string

Since the go language automatically adds semicolons after each line, it is important to note that when using multiple lines of string:

S: = "Starting part"
+ "Ending part"

will be automatically converted to:

S: = "starting part";
+ "Ending part";

This is wrong and should be written as:

S: = "Starting part" + "ending part"

So go does not insert semicolons in the wrong place, another way is to use the back quotation marks (') as the original string symbol:

S: = ' starting part
Ending part '

s also contains a line break. Because the characters in the middle of the anti-quote will remain unchanged.


String conversions

You can use a string (char) to convert a character to a string that contains a single character.


Package for string processing

---------------------------------------------

String Package

including string segmentation, substitution, merging and other processing;

StrConv Bag

Provides a number of conversion functions that can be used between strings and other types of data.

Utf-8 Bag

Used primarily to query and manipulate UTF-8 encoded strings or character slices.

Unicode Package

Provides some functions to check whether Unicode code points conform to the main criteria.

---------------------------------------------

Rune

Rune is the alias of Int32. Encode with UTF-8. When does this type work? For example, when you need to traverse a character string, you can mix each byte. To get the actual characters, you need to use the Rune type.

Output Result:

6

2 [20320 22909]

---------------------------------------------

Welcome to pay attention to code surgery! Learn Golang language together.



Related Article

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.