[Golang] String

2021-10-28 hit count image

Let's see how to define String and how to use it in Golang.

Outline

In this blog post, I will introduce how to define String variables and how to use it in Golang. You can see the full source code of this blog post on the link below.

String

In Golang, you can define the string variable like the below.

str1 := "Hello\nWorld!"
fmt.Println(str1)

Also, you can use \` to define multi-line string like the below.

str2 := `Hello
World!`
fmt.Println(str2)

To check these, create the main.go file and modify it like the below.

package main

import "fmt"

func main() {
  str1 := "Hello\nWorld!"
  fmt.Println(str1)

  str2 := `Hello
World!`
  fmt.Println(str2)
}

When you execute the code, you can see the following result.

# go run main.go
Hello
World!
Hello
World!

String traversal

In Golang, you can traverse the string like the below.

str1 := "Hello, world!"

for i := 0; i < len(str1); i++ {
  fmt.Printf("%c", str1[i])
}

However, the len() function returns the byte length of the string. So, when you use the len() function, the language that is not 1 byte character like Korean won’t be shown well(Korean > 3 bytes).

str2 := "Hello, 월드!"

for i := 0; i < len(str2); i++ {
  fmt.Printf("%c", str2[i])
}

To solve this issue, we can use rune(int32 alias) type to change the character to 4 bytes, and then we can print it.

str3 := "Hello, 월드!"
arr := []rune(str3)

for i := 0; i < len(arr); i++ {
  fmt.Printf("%c", arr[i])
}

To check these, modify the main.go file like the below.

package main

import "fmt"

func main() {
  str1 := "Hello, world!"

  for i := 0; i < len(str1); i++ {
    fmt.Printf("%c", str1[i])
  }

  fmt.Println()

  str2 := "Hello, 월드!"

  for i := 0; i < len(str2); i++ {
    fmt.Printf("%c", str2[i])
  }

  fmt.Println()

  str3 := "Hello, 월드!"
  arr := []rune(str3)

  for i := 0; i < len(arr); i++ {
    fmt.Printf("%c", arr[i])
  }
}

When the code is executed, you can see the result like the below.

# go run main.go
Hello, world!
Hello, ìë!
Hello, 월드!%

String operators

In Golang, you can use the following operators to manipulate the string.

String concatenation

In Golang, you can use the + operator to concatenate the string like the below.

package main

import "fmt"

func main() {
  str1 := "Hello"
  str2 := "world!"
  fmt.Println(str1 + " " + str2)

  str := "Hello"
  str += " " + "world!"
  fmt.Println(str)
}

To check this, modify the main.go file and execute it. You can see the following result.

# go run main.go
Hello world!
Hello world!

Comparison operators

You can use the comparison operators to compare the string like the below in Golang.

package main

import "fmt"

func main() {
  str3 := "a"
  str4 := "b"
  fmt.Println(str3 == str4)
  fmt.Println(str3 != str4)
  fmt.Println(str3 < str4)
  fmt.Println(str3 > str4)
}

When comparing the characters, the character code value is used for comparison.

  • A-Z: 65-90
  • a-z: 97-122

To check this, modify the main.go file and execute it. You can see the following result.

# go run main.go
false
true
true
false

String is pointer

In Golang, basically the same size of the data can be assigned. However, the string data size is dependent on the length of the string. But, the string can be assigned any string data againest Golang’s default rule. Because, the string is the pointer.

To check this, open the main.go file and modify it like the below.

package main

import (
  "fmt"
  "reflect"
  "unsafe"
)

func main() {
  str1 := "Hello, World!"
  str2 := str1

  strHeader1 := (*reflect.StringHeader)(unsafe.Pointer(&str1))
  strHeader2 := (*reflect.StringHeader)(unsafe.Pointer(&str2))

  fmt.Println(strHeader1)
  fmt.Println(strHeader2)
}

When you execute the code, you can see the result like the below.

&{17444156 13}
&{17444156 13}

As you see the result, two variable has the same memory address and same data size.

String is immutable

String is the immutable variable.

package main

import "fmt"

func main() {
  str := "Hello, World!"
  str = "Hello"
  fmt.Println(str)

  // str[0] = "a" // ERROR
}

So, we can re-assign all, but we can’t change one character of the string.

If, you want to change a character of the string, you can assign the string to new slice variable and modify it like the below.

package main

import "fmt"

func main() {
  var str1 string = "Hello, World!"
  var slice []byte = []byte(str1)

  slice[0] = "h"[0]
  fmt.Println(str1)
  fmt.Printf("%s\n", slice)
}

When the code is executed, you can see the result like the below.

# go run main.go
Hello, World!
hello, World!

strings package

When we manipulate the string in Golang , we can use the strings package provided Golang.

There are many features in the strings package and it is implemented effectively, so it’s better to use them instead of implementing them by yourself. So, if you make a program which manipulates the string, it’s better to check the strings package.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world!"
  fmt.Println(strings.ToUpper(str))
  fmt.Println(strings.Contains(str, "hello"))
  fmt.Println(strings.Index(str, "w"))
  fmt.Println(strings.Compare("a", "b"))
}

When you execute the code above, you can see the following result.

# go run main.go
HELLO, WORLD!
true
7
-1

Completed

Done! we’ve seen how to define the string variable and how to use it. Also, we’ve seen the characteristics of the string in Golang and how to use the strings package to manipulate the string.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts