相关疑难解决方法(0)

在不同类型的切片之间转换

[]byte从UDP套接字获得一个字节slice()并希望将其视为整数slice([]int32)而不更改底层数组,反之亦然.在C(++)中,我只是在指针类型之间转换; 我怎么在Go中这样做?

go go-reflect

40
推荐指数
4
解决办法
3万
查看次数

如何将uint8转换为字符串

http://play.golang.org/p/BoZkHC8_uA

我想将uint8转换为字符串,但无法弄清楚如何.

  package main

  import "fmt"
  import "strconv"

  func main() {

    str := "Hello"
    fmt.Println(str[1])  // 101

    fmt.Println(strconv.Itoa(str[1]))
  }
Run Code Online (Sandbox Code Playgroud)

这给了我 prog.go:11: cannot use str[1] (type uint8) as type int in function argument [process exited with non-zero status]

任何的想法?

go

11
推荐指数
3
解决办法
3万
查看次数

如何在golang中将字符串转换为整数

我想在golang中将字符串转换为整数.但我不知道字符串的格式.例如,"10"- > 10,"65.0"- > 65,"xx"- > 0,"11xx" - > 11,"xx11" - > 0

我做了一些搜索并找到了strconv.ParseInt().但它无法处理"65.0".所以我必须检查字符串的格式.

有没有更好的办法?

casting type-conversion go

9
推荐指数
3
解决办法
2万
查看次数

如何将 uint8 切片转换为字符串

[]uint8从 转换为的最佳方式是什么string

我正在使用http://github.com/confluenceinc/confluence-kafka-go/kafka

从 kafka 读取事件。但它不返回纯字符串事件。它返回类型为 的事件[]uint8。我怎样才能将此事件从 转换[]uint8string

type-conversion go

6
推荐指数
1
解决办法
9920
查看次数

将int8放入字节数组

我有以下字节数组:

buf := make([]byte, 1)
var value int8
value = 45
buf[0] = value // cannot use type int8 as type []byte in assignment
Run Code Online (Sandbox Code Playgroud)

当我想将一个char值放入字节数组时,我得到了错误cannot use type int8 as type []byte in assignment.怎么了?我该怎么做呢?

go

5
推荐指数
1
解决办法
3167
查看次数

标签 统计

go ×5

type-conversion ×2

casting ×1

go-reflect ×1