Go language type conversion


Release date:2023-09-19 Update date:2023-10-13 Editor:admin View counts:330

Label:

Go language type conversion

Type conversion is used to convert a variable of one data type to another. The basic format of Go language type conversion is as follows:

type_name(expression)

type_name is the type, expression is an expression.

Example

In the following example, an integer is converted to a floating-point type, the result is calculated, and the result is assigned to a floating-point variable:

Example

package main
import "fmt"
func main() {
   var sum int = 17
   var count int = 5
   var mean float32

   mean = float32(sum)/float32(count)
   fmt.Printf("The value of mean is: %f\\n",mean)
}

The output of the above example is as follows:

The value of mean is: 3.400000

Powered by TorCMS (https://github.com/bukun/TorCMS).