获取字符串中第一个字符的*uint8指针。

huangapple go评论91阅读模式
英文:

Get *uint8 pointer to a first character in a string

问题

我正在尝试使用unsafe.Pointer将一个哈希函数从C语言移植到Go语言。我需要获取键字符串的第一个字符的指针。

我尝试了&(hello[0])&hello[0],但都失败了。

英文:

I am trying to port a hash function from C to Go using unsafe.Pointer. I need to get the pointer to the first character of the key string.

I tried &(hello[0]) and &hello[0] and failed.

答案1

得分: 2

在Go语言中,字符串(string)的值是不可变的,因此不能对其符文或字节取地址。如果可以的话,你就可以修改指向的值,从而修改字符串的内容。在Go语言中也没有指针算术运算。

你可以通过简单地对字符串进行索引来实现类似指针算术的效果。通过索引字符串,你实际上是在索引它的字节(以UTF-8编码形式存储在内存中)。你可以用"索引算术"来模拟指针算术。

看下面的例子:

s := "Hello World"
i := 0
fmt.Println(s[i], string(s[i]))
i += 4
fmt.Println(s[i], string(s[i]))

输出结果(在Go Playground上尝试):

72 H
111 o

如果有需要,你还可以将字符串转换为[]byte[]rune,这样你就可以对其进行操作(例如,如果你需要修改字节)。切片也是可寻址的。请注意,将字符串值转换为这些切片类型会复制字符串的内容(以保持字符串的不可变性)。

虽然rune类型表示Unicode字符,但如果你想要复制哈希算法,你很可能希望使用字节,所以将其转换为字节切片(即字符串的UTF-8编码字节序列):

s := "Hello World"
b := []byte(s)
// 在这里你可以操作可寻址的切片b

请注意,在Go语言中,byte类型和uint8类型是"相同的",byteuint8的别名,你也可以这样写:

s := "Hello World"
b := []uint8(s)
英文:

string values in Go are immutable, and therefore not addressable; so you cannot take the address of its runes or bytes. If you could, you could modify the pointed value and thus modify the content of the string value. In Go there is also no pointer arithmetic.

What you can do is simply index the string, indexing a string indexes its bytes (in UTF-8 encoded form, this is how Go stores string values in memory). You can "mimic" the pointer arithmetic with "index arithmetic".

See this example:

s := "Hello World"
i := 0
fmt.Println(s[i], string(s[i]))
i += 4
fmt.Println(s[i], string(s[i]))

Output (try it on the Go Playground):

72 H
111 o

You may also convert the string to a []byte or []rune, should you have the need to do so (e.g. if you need to modify the bytes), and you can work with that. Slices are also addressable. Note that converting a string value to the mentioned slice types will make a copy of the string content (to preserve the immutability of the string).

While it's the rune type that represents a Unicode character, if you're trying to reproduce a hash algorithm, you most likely want to work with bytes, so convert it to a byte slice (which is the UTF-8 encoded byte sequence of the string):

s := "Hello World"
b := []byte(s)
// Here you can work with b which is addressable

Note that in Go, the types byte and uint8 are "identical", byte is an alias for uint8, you can also write:

s := "Hello World"
b := []uint8(s)

答案2

得分: 0

在Go语言中,字符串是字节的集合,需要将字符串转换为符文(rune)。

package main

import (
	"fmt"
)

func test(r *rune) {
	*r = rune('L')
}

func main() {
	s := []rune("Hello")
	fmt.Println(string(s))
	fmt.Println(&s[0])
	test(&s[0])
	fmt.Println(string(s))
}

示例代码在Go Playground中的链接

英文:

In golang string it's a massive of bytes, need convert string to rune

package main

import (
	"fmt"
)

func test(r *rune) {
    *r = rune('L')
}

func main() {
	s := []rune("Hello")
	fmt.Println(string(s))
	fmt.Println(&s[0])
	test(&s[0])
	fmt.Println(string(s))
}

Example playground golang

huangapple
  • 本文由 发表于 2017年2月28日 16:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42503775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定