Below
statement is used to convert byte array to string.
Approach 1: Using string()
Syntax
msg :=
string(byteArray[:])
App.go
package main import "fmt" func main() { arr := []byte{97, 98, 99, 100, 101, 102, 103} msg := string(arr) fmt.Println("msg : "
Output
msg : abcdefg
Approach 2: Using fmt.Sprintf
Syntax
msg :=
fmt.Sprintf("%s", arr)
App.go
package main import "fmt" func main() { arr := []byte{97, 98, 99, 100, 101, 102, 103} msg := fmt.Sprintf("%s", arr) fmt.Println("msg : ", msg) }
Approach 3: Using bytes.NewBuffer
Syntax
msg := bytes.NewBuffer(byteArray).String()
App.go
package main import ( "bytes" "fmt" ) func main() { byteArray := []byte{97, 98, 99, 100, 101, 102, 103} msg := bytes.NewBuffer(byteArray).String() fmt.Println("msg : ", msg) }
Previous Next Home
No comments:
Post a Comment