Converting to string type is often used but you don’t know how to do it? Let’s learn it together.
Convert another type to string
It often needs to be done to convert a non-string type to string. Let’s check how to do it.
Int to string
You might have this experience. The conversion can be done by string(intValue)
but it is converted to ASCII code.
intValue := 99
intStr1 := string(intValue)
fmt.Printf("intStr1: %s\n", intStr1) // c
If you want to check the table, go to this web site.
In most cases, this is not the expected result. We should use fmt.Sprint()
function instead.
intStr2 := fmt.Sprint(intValue)
fmt.Printf("intStr2: %s\n", intStr2) // 99
This is what we actually want.
Float to string
If you try to convert float value to string by string()
, you get an error.
floatValue := 12.345
floatStr := string(floatValue)
// cannot convert floatValue (variable of type float64) to string
However, fmt.Sprint()
function can be used for float value too.
floatValue := 12.345
floatStr := fmt.Sprint(floatValue)
fmt.Printf("floatStr: %s\n", floatStr) // 12.345
Array to string
Likewise, use fmt.Sprint()
.
arrayValue := [5]int{1, 2, 3, 4, 5}
arrayStr := fmt.Sprint(arrayValue)
fmt.Printf("arrayValue: %s\n", arrayStr) // arrayValue: [1 2 3 4 5]
Byte to string
We can use the same functions here but it depends on your expected result which function to use.
rawBytes := []byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58}
fmt.Printf("%s\n", rawBytes) // 0123456789
fmt.Printf("%s\n", string(rawBytes)) // 0123456789
fmt.Printf("%s\n", bytes.NewBuffer(rawBytes).String()) // 0123456789
If you want to use it as a human readable string, the two functions above can be used.
If you need the raw byte codes, use fmt.Sprint()
.
rawByteString := fmt.Sprint(rawBytes)
fmt.Printf("%s\n", rawByteString) // [48 49 50 51 52 53 54 55 56 57 58]
Struct to string
Do you need struct too for … maybe logging? OK, let’s try.
type myStruct struct {
name string
age uint64
}
myStruct := myStruct{name: "Yuto", age: 35}
fmt.Printf("%%v: %v\n", myStruct) // %v: {Yuto 35}
fmt.Printf("%%T: %T\n", myStruct) // %T: utils.myStruct
fmt.Printf("%%#v: %#v\n", myStruct) // %#v: utils.myStruct{name:"Yuto", age:0x23}
fmt.Println(myStruct) // {Yuto 35}
Hmm… None of them doesn’t return our expected result. %#v
is nearly what we want but the int value is written as HEX.
But… official site describes about %+v
.
%v the value in a default format
https://pkg.go.dev/fmt
when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value
https://pkg.go.dev/fmt
It works.
fmt.Printf("%%+v: %+v\n", myStruct) // %+v: {name:Yuto age:35}
Then, let’s use fmt.Sprintf()
function instead of fmt.Sprint()
.
struct_v := fmt.Sprintf("%+v", myStruct)
fmt.Println(struct_v) // {name:Yuto age:35}
There is another way if you want to make it more readable. The way is however to define it yourself.
func (m myStruct) String() string {
return fmt.Sprintf("{ name: %s, age: %d }", m.name, m.age)
}
structStr := fmt.Sprint(myStruct)
fmt.Println(structStr) // { name: Yuto, age: 35 }
The String()
is defined in the following way in fmt
package.
type Stringer interface {
String() string
}
This method is called in the case where the string conversion is needed.
Extract specified ranged string by using slice
If you need to extract string in a certain range from the existing string, you can use slice.
numberStr := "0123456789"
slices := numberStr[2:8]
fmt.Println(slices) // 234567
Comments