go2021년 1월 16일1 min read

Type Conversion in Go

How explicit type conversion works in Go and why Go only supports it.

FFrank Advenoh
#golang#type#conversion
Contents · 1

Type conversion means changing a value's data type. Java supports both explicit type conversion and implicit type conversion, but Go supports only explicit type conversion.

The type conversion syntax converts the value val to type T, as shown below.

T(val)

In the example, an int value is converted to the float64 and uint32 types.

func Example_TypeConversion() {
	var i = 52
	var j float64 = float64(i)
	var k = uint32(j)

	fmt.Println(i)
	fmt.Println(j)
	fmt.Println(k)

	//Output:
	//52
	//52
	//52
}

You can find the code written in this post on github.

References

관련 글