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

报告相同问题?

悬赏问题

  • ¥15 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架