How to append a character to a string in Go?
This does not work:
s := "hello"; c := 'x'; fmt.Println(s + c);
invalid operation: s + c (mismatched types string and rune)
This does not work either:
s := "hello"; c := 'x'; fmt.Println(s + rune(c));
invalid operation: s + rune(c) (mismatched types string and rune)
Go add strings with + operator The + and the += operators provide the easiest way of concatenating strings. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded. Two strings are added with the + operator.
Using += operator or String append: In Go strings, you are allowed to append a string using += operator. This operator adds a new or given string to the end of the specified string. Using Join() function: This function concatenates all the elements present in the slice of string into a single string.
Since slices are dynamically-sized, you can append elements to a slice using Golang's built-in append method. The first parameter is the slice itself, while the next parameter(s) can be either one or more of the values to be appended.
In Golang strings, you can check whether the string begins with the specified prefix or not with the help of HasPrefix() function. This function returns true if the given string starts with the specified prefix and return false if the given string does not start with the specified prefix.
In Go rune type is not a character type, it is just another name for int32.
If you come from Java or a similar language this will surprise you because Java has char type and you can add char to a string.
String s = "hello"; char c = 'x'; System.out.println(s + c);
In Go you need to be more explicit:
s := "hello"; c := 'x'; fmt.Println(s + string(c));
Omg do you really need to convert every char to a string constant? Yes, but do not worry, this is just because of a type system and compiler optimizes it correctly. Under the hood both Java and Go append the char in the same manner.
If you think extra typing sucks, just compare how many times string keyword appears in each example above. :)
Extra info: (technical details)
In Go strings are not sequences of runes, they are utf-8 encoded sequences of runes. When you range over a string you get runes, but you cannot simply append a rune to a string. For example: euro sign '€' is an integer 0x20AC (this is called code point) But when you encode euro sign in utf-8 you get 3 bytes: 0xE2 0x82 0xAC http://www.fileformat.info/info/unicode/char/20aC/index.htm
So appending a char actually works like this:
s = append(s, encodeToUtf8(c)) // Go s = append(s, encodeToUtf16(c)) // Java
Note that encodings are done at compile time.
Utf-8 can encode a character with 1, 2, 3, or 4 bytes. Utf-16 can encode a character with 2 or with 4 bytes.
So Go usually appends 1 byte (for ascii) or 2, 3, 4 bytes for Chinese, and Java usually appends 2 bytes (for ascii) or 4 bytes for Chinese.
Since most characters that we (west) use can be encoded with 2 bytes Java gives the false belief that strings are sequences of 2byte char-s, which is true until you need to encode 美国必须死
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With