drwg89980 2016-09-28 17:33
浏览 60
已采纳

为什么在Go中将字节切片的元素作为整数返回?

On the go playground, this code

package main

import (
    "fmt"
)

var a = []byte{ 0xff, 0xaa, 0x66, 0x33, 0x00 }

func main() {
    for b := range a {
        fmt.Printf("%T
", b)
    }
}

prints

int
int
int
int
int

Why in the world wouldn't a slice of bytes give you bytes when you iterate through it (or at least uint8 or something)?

Here's the link to the playground: https://play.golang.org/p/V1uZZWWq-X.

  • 写回答

2条回答 默认 最新

  • dongyun8138 2016-09-28 18:01
    关注

    for i := range thing sets i to indices into the thing (unlike Python or such). for i, b := range byteslice would put the index in i and byte in b, and for _, b := range byteslice would just put the byte in b.

    The spec on for statements lays out all the different special cases related to range expressions (including strings, channels, and maps as well).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料