douwei6478 2015-11-16 06:57 采纳率: 100%
浏览 5416
已采纳

在Go中将uint32转换为int

如何将uint32转换到 int in Go?

一个小的背景,我正在从文件中读取内容,这让我从字节数组中获得正确的大小,如下所示:

size := binary.BigEndian.Uint32(b[4:])

但是 bufio.Discard 该func期望一个int。 我可以使用fmt将其大小转换为字符串,然后使用strconv将其转换为int,必须有更优雅的方法才能做到这一点.

  • 写回答

1条回答 默认 最新

  • doujiene2845 2015-11-16 11:36
    关注

    The Go Programming Language Specification

    Conversions

    Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.

    For example,

    size := binary.BigEndian.Uint32(b[4:])
    n, err := rdr.Discard(int(size))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 请回答用户的提问 10月16日