xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Go type conversions All In One

Go type conversions All In One

Go 语言类型转换

https://go.dev/ref/spec#Conversions

*Point(p)        // same as *(Point(p))
(*Point)(p)      // p is converted to *Point
<-chan int(c)    // same as <-(chan int(c))
(<-chan int)(c)  // c is converted to <-chan int
func()(x)        // function signature func() x
(func())(x)      // x is converted to func()
(func() int)(x)  // x is converted to func() int
func() int(x)    // x is converted to func() int (unambiguous)

uint(iota)               // iota value of type uint
float32(2.718281828)     // 2.718281828 of type float32
complex128(1)            // 1.0 + 0.0i of type complex128
float32(0.49999999)      // 0.5 of type float32
float64(-1e-1000)        // 0.0 of type float64
string('x')              // "x" of type string
string(0x266c)           // "♬" of type string
myString("foo" + "bar")  // "foobar" of type myString
string([]byte{'a'})      // not a constant: []byte{'a'} is not a constant
(*int)(nil)              // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
int(1.2)                 // illegal: 1.2 cannot be represented as an int
string(65.0)             // illegal: 65.0 is not an integer constant

The Go Programming Language Specification

Version of June 29, 2022

https://go.dev/ref/spec

strconv

ParseBool,ParseFloat,ParseInt 和 ParseUint 将字符串转换为

// ParseBool,ParseFloat,ParseInt 和 ParseUint 将`字符串`转换为`值`: 

b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)

formatUint 将转换为字符串

// formatUint将值转换为字符串: 

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

int to string

image


/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2023-01-07
 *
 * @description close-channel.go
 * @augments
 * @example
 * @link
 *
 */

package main

// import "fmt"

import (
  "fmt"
  // 字符串类型转换
  "strconv"
)

func fibonacci(n int, c chan int) {
  x, y := 0, 1
  for i := 0; i < n; i++ {
    c <- x
    // switch
    x, y = y, x + y
  }
  // 关闭通道
  close(c)
}

func main() {
  c := make(chan int, 10)
  go fibonacci(cap(c), c)
  // range 函数遍历每个从通道接收到的数据
  // 因为 c 在发送完 10 个数据之后就关闭了通道,所以这里我们 range 函数在接收到 10 个数据之后就结束了。
  // 如果上面的 c 通道不关闭,那么 range 函数就不会结束,从而在接收第 11 个数据的时候就阻塞了。
  for i := range c {
    // fmt.Println("fibonacci =", i)
    // int to string ✅
    str := strconv.Itoa(i)
    fmt.Println("fibonacci " + str + " =", i)
    // fmt.Println("fibonacci " + string(i) + " =", i)
    // ❌ invalid operation: "fibonacci " + i (mismatched types untyped string and int)
    // fmt.Println("fibonacci " + i + " =", i)
  }
  // ❌ range over c (variable of type chan int) permits only one iteration variable
  // for i, v := range c {
  //   fmt.Println("i, v =", i, v)
  // }
}

/*
$ go run close-channel.go
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci 1 = 1
fibonacci 2 = 2
fibonacci 3 = 3
fibonacci 5 = 5
fibonacci 8 = 8
fibonacci 13 = 13
fibonacci 21 = 21
fibonacci 34 = 34

*/

/*
$  go run close-channel.go
fibonacci = 0
fibonacci = 1
fibonacci = 1
fibonacci = 2
fibonacci = 3
fibonacci = 5
fibonacci = 8
fibonacci = 13
fibonacci = 21
fibonacci = 34

*/


/*
$ go run close-channel.go
fibonacci  = 0
fibonacci  = 1
fibonacci  = 1
fibonacci  = 2
fibonacci  = 3
fibonacci  = 5
fibonacci = 8
 = 13 cci
fibonacci  = 21
fibonacci " = 34 "

*/

image

string to int

demos

string 转 int & int 转 string

// string 转 int & int 转 string

package main
import (
  "fmt"
  "strconv"
)

func main() {
  // string to int ✅ (ASCII to int)
  aStr := "100"
  bInt, err := strconv.Atoi(aStr)

  if err == nil {
    fmt.Printf("aStr:%T %s,bInt:%T %d", aStr, aStr, bInt, bInt)
  } else {
    fmt.Printf("err:%s", err)
  }

  // int to string ✅ (in to ASCII)
  cInt := 200
  dStr := strconv.Itoa(cInt)

  fmt.Printf("cInt:%T %d,dStr:%T %s", cInt, cInt, dStr, dStr)
}

ASCII (American Standard Code for Information Interchange):美国信息交换标准代码

image

Go Docs

Documentation - The Go Programming Language

https://go.dev/doc/

tutorial

https://go.dev/doc/tutorial/getting-started

tour

https://go.dev/tour/list

https://go.dev/tour/welcome/1

https://go.dev/tour/basics/1

zh-Hans

https://tour.go-zh.org/list

https://tour.go-zh.org/welcome/1

https://tour.go-zh.org/basics/1

playground

https://go.dev/play/

https://go.dev/blog/playground

image

Go by Example

https://gobyexample.com/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://www.sololearn.com/learning/1164

https://www.runoob.com/go/go-type-casting.html

https://cloud.tencent.com/developer/section/1144302

https://stackoverflow.com/collectives/go



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-01-08 10:18  xgqfrms  阅读(39)  评论(8编辑  收藏  举报