Skip to content

Instantly share code, notes, and snippets.

@jiaqi-yin
Last active December 17, 2021 23:04
Show Gist options
  • Save jiaqi-yin/ae50aff587f909e83bd771de3054d995 to your computer and use it in GitHub Desktop.
Save jiaqi-yin/ae50aff587f909e83bd771de3054d995 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
// string to []byte
str := "hello, world"
b := []byte(str)
fmt.Printf("%T %v ~> %T %v\n", str, str, b, b)
// []byte to string
s := string(b)
fmt.Printf("%T %v ~> %T %v\n", b, b, s, s)
// number to string
i := 123
is := strconv.Itoa(i)
fmt.Printf("%T %v ~> %T %v\n", i, i, is, is)
ii := 456
iis := fmt.Sprintf("%d", ii)
fmt.Printf("%T %v ~> %T %v\n", ii, ii, iis, iis)
// string concatenation
strc := "abc"
strc = strc + "def"
fmt.Println(strc)
var sb strings.Builder
sb.WriteString("abc")
sb.WriteString("def")
fmt.Println(sb.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment