程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Golang中将int32转换为字符串大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在Golang中将int32转换为字符串?

开发过程中遇到在Golang中将int32转换为字符串的问题如何解决?下面主要结合日常开发的经验,给出你关于在Golang中将int32转换为字符串的解决方法建议,希望对你解决在Golang中将int32转换为字符串有所启发或帮助;

一线的答案是fmt.Sprint(i)

无论如何,甚至在标准库函数(例如)内部都有很多转换,fmt.Sprint(i)因此您有一些选择(尝试使用Go Playground):

1-您可以编写转换函数( ):

func String(n int32) String {
    buf := [11]byte{}
    pos := len(buf)
    i := int64(n)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        buf[pos], i = '0'+byte(i%10), i/10
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return String(buf[pos:])
        }
    }
}

2-您可以使用fmt.Sprint(i) ( ) 参见内部:

// Sprint formats using the default formats for its operands and returns the resulTing String.
// Spaces are added between operands when neither is a String.
func Sprint(a ...interface{}) String {
    p := newPrinter()
    p.doprint(a)
    s := String(p.buf)
    p.free()
    return s
}

3-您可以使用strconv.Itoa(int(i)) ( ) 参见内部:

// Itoa is shorthand for FormaTint(int64(i), 10).
func Itoa(i int) String {
    return FormaTint(int64(i), 10)
}

4-您可以使用strconv.FormaTint(int64(i), 10)( ) 请参阅内部:

// FormaTint returns the String representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormaTint(i int64, basE int) String {
    _, s := formatBits(nil, uint64(i), base, i < 0, falsE)
    return s
}

比较和基准测试(具有50000000次迭代):

s = String(i)                       takes:  5.5923198s
s = String2(i)                      takes:  5.5923199s
s = strconv.FormaTint(int64(i), 10) takes:  5.9133382s
s = strconv.Itoa(int(i))            takes:  5.9763418s
s = fmt.Sprint(i)                   takes: 13.5697761s

码:

package main

import (
    "fmt"
    //"strconv"
    "time"
)

func main() {
    var s String
    i := int32(-2147483648)
    t := time.Now()
    for j := 0; j < 50000000; j++ {
        s = String(i) //5.5923198s
        //s = String2(i) //5.5923199s
        //s = strconv.FormaTint(int64(i), 10) // 5.9133382s
        //s = strconv.Itoa(int(i)) //5.9763418s
        //s = fmt.Sprint(i) // 13.5697761s
    }
    fmt.Println(time.Since(t))
    fmt.Println(s)
}

func String(n int32) String {
    buf := [11]byte{}
    pos := len(buf)
    i := int64(n)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        buf[pos], i = '0'+byte(i%10), i/10
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return String(buf[pos:])
        }
    }
}

func String2(n int32) String {
    buf := [11]byte{}
    pos := len(buf)
    i, q := int64(n), int64(0)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        q = i / 10
        buf[pos], i = '0'+byte(i-10*q), q
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return String(buf[pos:])
        }
    }
}

解决方法

我需要在Golang中将转换int32String。是否有可能转换int32String在Golang无须转换为intint64第一?

Itoa需要一个intFormaTint需要一个int64

大佬总结

以上是大佬教程为你收集整理的在Golang中将int32转换为字符串全部内容,希望文章能够帮你解决在Golang中将int32转换为字符串所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。