Home > Backend Development > Golang > How to type convert in golang

How to type convert in golang

(*-*)浩
Release: 2019-12-31 09:09:26
Original
1927 people have browsed it

How to type convert in golang

How to convert in Go language

In Go language, there is only explicit conversion, there is no implicit conversion (recommended learning: go)

Conversion format: data type (converted data)

var num float64 = 3.14
var value int = int(num)
fmt.Printf("%d\n", value)
Copy after login

Notes

Data type (converted data) format is general Used for conversion between other basic data types except string and Boolean types

No implicit type conversion

//var num int = 3.14  会报错
Copy after login

Conversion between basic data types and string

Use the fmt.sprintf function for

package main
 
import "fmt"
 
func main() {
    var x1 int = 88
    var x2 float32 = 3.45
    var x3 string
 
    x3 = fmt.Sprintf("this is a int  %d \n", x1)   //注意一定要使用双引号
    fmt.Print(x3)
    x3 = fmt.Sprintf("this is a  float str %f \n", x2)
    fmt.Print(x3)
}
Copy after login

The above is the detailed content of How to type convert in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template