3 Different Ways to Convert Golang Int to String

How to Convert Golang Int to String

There are three ways to convert an int to a string in Golang. strconv.Itoa(): The strconv.Itoa() function converts an integer base 10 value to an ASCII string. strconv.FormatInt(): The strconv.Format() function converts the int64 value to a string. fmt.Sprintf(): The fmt.Sprintf() function formats according to a format specifier and returns the resulting string. Golang string is a … Read more

How to Convert Byte Array to String in Golang

How to Convert Byte Array to String in Golang

There are the following methods to convert a byte array to a string. Method 1: Using the string() constructor Method 2: Using the bytes.NewBuffer() function Method 3: Using the fmt.Sprintf() function Method 1: Using the string() constructor To convert a byte array to a string in Golang, you can use the string() constructor. The string() … Read more

How to Convert String to Int in Golang

How to Convert Golang String to Int

To convert a string to int in Golang, you can use the strconv.Atoi() function. The strconv.Atoi() is a built-in function that converts string data type into an integer. Example package main import ( “fmt” “strconv” ) func main() { str := “1921” op, err := strconv.Atoi(str) if err != nil { panic(err) } fmt.Printf(“%T \n%v … Read more

How to Convert Golang String to JSON

How to Convert Golang String to JSON

To convert a string to json in Golang, you can use the json.Marshal() function. The json.Marshal() is a json package’s function that encodes objects in JSON format known as marshaling. package main import ( “encoding/json” “fmt” “log” “reflect” ) func main() { str := “here is the data” jsondata, err := json.Marshal(str) if err != nil … Read more