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

在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日

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!