在Go中将uint32转换为int

在Go中将uint32转换为int

问题描述:

如何将uint32转换到 int in Go?

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

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

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

如何将 uint32 code>转换为 int code> p>

有一点背景,我正在读取文件,这使我可以从字节数组中获得正确的大小,如下所示: p>

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

但是, bufio.Discard code>函数需要一个 INT。 我能够使用fmt将大小转换为字符串,然后使用strconv将其转换为int。 必须有一种更优雅的方法来做到这一点。 p> div>

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))