package main

import (
"fmt"
"strconv"
) func main() {
strVar := "100"
intVar, _ := strconv.Atoi(strVar) strVar1 := "-52541"
intVar1, _ := strconv.ParseInt(strVar1, 10, 32) strVar2 := "101010101010101010"
intVar2, _ := strconv.ParseInt(strVar2, 10, 64) fmt.Println(intVar, intVar1, intVar2)
}

 Package strconv implements conversions to and from string representations of basic data types. Atoi is equivalent to ParseInt(s, 10, 0), converted to type int. ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.

How to Convert string to float type in Go?

ParseFloat converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.

package main

import (
"fmt"
"strconv"
) func main() {
s := "3.1415926535"
f, _ := strconv.ParseFloat(s, 8)
fmt.Printf("%T, %v\n", f, f) s1 := "-3.141"
f1, _ := strconv.ParseFloat(s1, 8)
fmt.Printf("%T, %v\n", f1, f1) s2 := "-3.141"
f2, _ := strconv.ParseFloat(s2, 32)
fmt.Printf("%T, %v\n", f2, f2)
}

  

float64, 3.1415926535
float64, -3.141
float64, -3.1410000324249268

 

String to Boolean Data Type Conversion in Go

ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

package main

import (
"fmt"
"strconv"
) func main() {
s1 := "true"
b1, _ := strconv.ParseBool(s1)
fmt.Printf("%T, %v\n", b1, b1) s2 := "t"
b2, _ := strconv.ParseBool(s2)
fmt.Printf("%T, %v\n", b2, b2) s3 := "0"
b3, _ := strconv.ParseBool(s3)
fmt.Printf("%T, %v\n", b3, b3) s4 := "F"
b4, _ := strconv.ParseBool(s4)
fmt.Printf("%T, %v\n", b4, b4)
}

  

bool, true
bool, true
bool, false
bool, false

Convert Boolean Type to String in Go

FormatBool function used to convert Boolean variable into String.

package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
var b bool = true
fmt.Println(reflect.TypeOf(b)) var s string = strconv.FormatBool(b)
fmt.Printf("%T, %v\n", s, s)
fmt.Println(reflect.TypeOf(s))
}

  

bool

string, true

string

How to Convert Float to String type in Go?

FormatFloat converts the floating-point number f to a string s.
package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
var f float64 = 3.1415926535
fmt.Println(reflect.TypeOf(f))
fmt.Println(f) var s string = strconv.FormatFloat(f, 'E', -1, 32)
fmt.Println(reflect.TypeOf(s))
fmt.Println(s)
}

  

float64
3.1415926535
string
3.1415927E+00

Convert Integer Type to String in Go

FormatInt converts the Integer number i to a String s.
package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
var i int64 = -654
fmt.Println(reflect.TypeOf(i))
fmt.Println(i) var s string = strconv.FormatInt(i, 10)
fmt.Println(reflect.TypeOf(s))
fmt.Println(s)
}

  

int64
-654
string
-654

Convert Int to Int16 Int32 Int64 in Golang

package main

import (
"fmt"
"reflect"
) func main() {
var i int = 10
fmt.Println(reflect.TypeOf(i)) i16 := int16(i)
fmt.Println(reflect.TypeOf(i16)) i32 := int32(i)
fmt.Println(reflect.TypeOf(i32)) i64 := int64(i)
fmt.Println(reflect.TypeOf(i64))
}

  

int
int16
int32
int64

Convert Float32 to Float64 and Float64 to Float32 in Golang

package main

import (
"fmt"
"reflect"
) func main() {
var f32 float32 = 10.6556
fmt.Println(reflect.TypeOf(f32)) f64 := float64(f32)
fmt.Println(reflect.TypeOf(f64)) f64 = 1097.655698798798
fmt.Println(f64) f32 = float32(f64)
fmt.Println(f32)
}

  

float32
float64
1097.655698798798
1097.6556

Convert Int to Float in Golang

package main

import (
"fmt"
"reflect"
) func main() {
var f32 float32 = 10.6556
fmt.Println(reflect.TypeOf(f32)) i32 := int32(f32)
fmt.Println(reflect.TypeOf(i32))
fmt.Println(i32) f64 := float64(i32)
fmt.Println(reflect.TypeOf(f64))
}

  

float32
int32
10
float64
 

golang --Converting and Checking Types的更多相关文章

  1. Checking Types Against the Real World in TypeScript

    转自:https://www.olioapps.com/blog/checking-types-real-world-typescript/ This is a follow-up to Type-D ...

  2. 理解golang中的function types

    先找个例子来看一下: package main import "fmt" // Greeting function types type Greeting func(name st ...

  3. golang map学习

    当对map只声明时,由于map为引用类型,所以默认值为nil,但对nil map 而言,支持read ,但不支持write 当执行write操作时, 会抛出panic异常; 代码如下: func Te ...

  4. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  5. Microsoft SQL Server Trace Flags

    Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...

  6. 10 The Go Programming Language Specification go语言规范 重点

    The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...

  7. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  8. SWIG 3 中文手册——6. SWIG 和 C++

    目录 6 SWIG 和 C++ 6.1 关于包装 C++ 6.2 方法 6.3 支持的 C++ 功能 6.4 命令行选项与编译 6.5.1 代理类的构造 6.5.2 代理类中的资源管理 6.5.3 语 ...

  9. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

随机推荐

  1. 2019.10.1 qbxt模拟题

    第一题 考虑树上\(DP\),f[i][j][0/1]表示以\(i\)为根的子树,入读为零点的个数为\(j\),点\(i\)的入度为\(0\)/不为\(0\)时的方案数 转移的时候考虑\(u\)的一个 ...

  2. 【BIEE】导出大量数据报错处理请求时出现致命错误。服务器响应为 com.siebel.analytics.utils.InputStreamWithLimit$ReadOverTheLimitException

    问题描述 在使用BIEE导出大量数据的时候,报错如下: 处理请求时出现致命错误.服务器响应为: com.siebel.analytics.utils.InputStreamWithLimit$Read ...

  3. FZU Monthly-201906 tutorial

    FZU Monthly-201906 tutorial 题目(难度递增) easy easy-medium medium medium-hard hard 思维难度 AE B DG CF H A. X ...

  4. Tensorflows安装(cpu版安装方法)

    一.说明 首先声明,本人系统是Windows10 64位,Win7未试. 本文旨在帮助园友以更简单的方式安装Tensorflow,下面介绍的是如何安装Python的Tensorflow cpu版本. ...

  5. npm和yarn常用调试命令

    yarn查看全局安装路径 yarn global dir npm查看所有全局安装的包<全局路径也会显示> npm list --depth=0 -global

  6. pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

    使用pytesseract识别验证码中遇到异常如下: pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installe ...

  7. java中为什么notify()可能会导致死锁,而notifyAll()则不会

    简单的说,notify()只唤醒一个正在等待的线程,当该线程执行完以后施放该对象的锁,而没有再次执行notify()方法,则其它正在等待的线程 则一直处于等待状态,不会被唤醒而进入该对象的锁的竞争池, ...

  8. Ant Design Pro Vue 时间段查询 问题

    <a-form-item label="起止日期" :labelCol="{lg: {span: 7}, sm: {span: 7}}" :wrapper ...

  9. github 体积限制

    github 体积限制 仅供了解,切勿滥用.希望自觉维护良好社区. 单文件 单个文件大于 50M 时会收到警告, 100M 时会被拒绝. 该警告将告诉您哪些文件太大: remote: warning: ...

  10. Hadoop学习笔记——HDFS

    1.查看hdfs文件的block信息 不正常的文件 hdfs fsck /logs/xxx/xxxx.gz.gz -files -blocks -locations Connecting to nam ...