WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Intermediate · question 38 of 100

What are the rules for converting between different data types in Go?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

In Go, you cannot directly convert between different data types implicitly, like you can in some other programming languages. Instead, you need to do explicit type conversions. This can be both a blessing and a curse because, on the one hand, it makes code safer and less error-prone, but on the other hand, it can make code more verbose.

Here are the general rules for converting between different data types in Go:

1. Numerical types conversion: To convert from one numeric type to another (for example, from int to float64), you can use explicit type conversion. Keep in mind that when converting between different numeric types, there is a chance of losing information, especially when converting from a wider type to a narrower type (for example, from float64 to int). You need to be aware of the possible loss of information while converting.

var i int = 42
var f float64 = float64(i) // Conversion from int to float64
var u uint = uint(f) // Conversion from float64 to uint

2. String to byte slice conversion and the inverse: To convert a string to a byte slice and vice versa, you can use explicit type conversion without any extra function. The conversion is fast and efficient because it doesn’t allocate new memory but only creates a new header to reference the same underlying memory.

s := "Hello, World!"
b := []byte(s) // Conversion from string to byte slice
s2 := string(b) // Conversion back from byte slice to string

3. Rune and string conversion: To convert a string with a single character to a rune (an alias for int32), you can use ‘[]rune(s)‘ conversion.

s := "ä"
r := []rune(s)[0] // Convert string to rune
s2 := string(r) // Convert rune back to string

4. Conversion between different data structures (for example, slice, array, or map): No direct conversion between data structures is available. You need to loop through the elements of one data structure and add them individually to the other structure.

arr := [3]int{1, 2, 3}
slice := make([]int, len(arr))

for i, v := range arr {
    slice[i] = v // Copying elements from array to the slice
}

5. Interface conversion: You can convert a value of type interface to another type using type assertion or type switch statement.

var x interface{}
x = 42

i, ok := x.(int) // Type assertion, tries to convert x to int
if ok {
    fmt.Println("x is of type int:", i)
} else {
    fmt.Println("x is not of type int")
}

It’s important to remember that Go requires explicit type conversions, which means you need to be aware of the type of variables and their compatibility. Errors can occur if you try to perform invalid conversions or lose information while converting from a wider type to a narrower type.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics