Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert uint32 to int in Go

Tags:

go

How do I convert a uint32 into an int in Go?

A little background, I'm reading from a file and this gives me the correct size from a byte array like this:

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

However, the bufio.Discard func expects an int. I was able to use fmt to convert size to a string and then use strconv to get it to an int. There has to be a more elegant way to do this.

like image 415
Brian Boyd Avatar asked Nov 16 '15 06:11

Brian Boyd


1 Answers

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))
like image 148
peterSO Avatar answered Nov 06 '22 07:11

peterSO