Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a character to a string in Golang?

Tags:

string

concat

go

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)

like image 410
cohadar Avatar asked Oct 28 '16 17:10

cohadar


People also ask

How do I add a character to a string in Golang?

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.

Can you append to a string in Golang?

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.

How do I append in Golang?

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.

Does Golang have a prefix?

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.


1 Answers

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 美国必须死

like image 89
cohadar Avatar answered Sep 19 '22 23:09

cohadar