On string and array _golang in go language

Source: Internet
Author: User

The string processing in the go language is not the same as PHP and Java processing, first declaring the string and modifying the string

Copy Code code as follows:

Package Main
Import "FMT"
var name string//declaration of a string
var emptyname string = ""//Declaration of an empty string
Func Main () {
Declare multiple strings and assign values
A, B, V: = "Hello", "word", "Widuu"
Fmt. Println (A, B, v)
Converts the contents of a string by first converting the type A to []byte
c: = []byte (a)
assigning values
C[0] = ' n '
In converting to a string type, we actually find that our A has not changed
But the change of a new string
D: = string (c)
Fmt. Println (d)
String prototype output
M: = ' Hello
Word '
Fmt. Println (M)
}

How to declare an array

Copy Code code as follows:

Package Main
Import "FMT"
var arr [2]int//Declaration of an array
Func Main () {
ARR[0] = 1//array assignment
Fmt. Println (arr)
Arrtest: = [3]int{1, 2, 3}//array another way of declaring
Fmt. Println (Arrtest)
A: = [...] Int{1, 2}//[...] Automatically recognize the length of an array
Fmt. Println (a)
Fmt. Println (Len (a))//output array length
}

Below is the declaration and use of slice in fact, this is a dynamic array

Copy Code code as follows:

Package Main
Import "FMT"
Func Main () {
D: = []int{1, 2, 3}//Declare a slice this is a dynamic array, no length
Fmt. Println (d)
var q, W []int
Q = d[0:1]//Can be determined to get the length of the top
W = D[1:3]
D = Append (d, 2)//add element to it
Fmt. Println (d)
Fmt. PRINTLN (q, W)
}

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.