获取字符串指针的简单方法

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

Easy way to get string pointers

问题

你好!根据你的要求,我将为你翻译以下内容:

我正在使用的一个库有一个非常奇怪的API,经常需要使用字符串指针。目前我是这样做的:

s := "foobar"
weirdFun(&s)

来传递字符串。有没有一种方法可以在不使用变量的情况下完成这个操作?

英文:

A library I am using has a very weird API that often takes string pointers. Currently I am doing this:

s := "foobar"
weirdFun(&s)

to pass strings. Is there a way to do this without the variable?

答案1

得分: 1

Azure SDK使用字符串指针来区分没有值和空字符串。

使用Azure的StringPtr函数来创建指向字符串字面量的指针。

import (
    
	"github.com/Azure/go-autorest/autorest/to"
)


res, err := someClient.Create(ctx, someService.ExampleParameters{
	Location: to.StringPtr(location),
})
英文:

The Azure SDK uses string pointers to distinguish between no value and the empty string.

Use Azure's StringPtr function to create a pointer to a string literal.

import (
    ⋮
	"github.com/Azure/go-autorest/autorest/to"
)

⋮
res, err := someClient.Create(ctx, someService.ExampleParameters{
	Location: to.StringPtr(location),
})

答案2

得分: 1

这个库真的很奇怪,但是你可以使用函数wrap在一行中完成这个操作,例如:

func PointerTo[T ~string](s T) *T {
	return &s
}

s := "string"

weirdFun(PointerTo(s))
英文:

The library is really weird, but
you can do this in one line with function wrap, for example

func PointerTo[T ~string](s T) *T {
	return &s
}

s := "string"

weirdFun(PointerTo(s))

答案3

得分: 0

也许你应该告知库的作者,在Go语言中,字符串已经是引用类型(指向一个内部表示为rune切片的结构体),因此将字符串传递给函数时不会进行昂贵的复制操作,它是按引用传递的。
希望这能帮到你!

英文:

Maybe you should inform the author of the library, that the strings in Go are already references (to a structure, which is internally represented as a slice of runes), so no expensive copy operation is made by passing string to a function, it's call by reference.
Hope this helps!

答案4

得分: 0

地址操作&x可以与可寻址的值一起使用。
根据语言规范

操作数必须是可寻址的,也就是说,要么是变量、指针间接引用或切片索引操作;要么是可寻址结构体操作数的字段选择器;要么是可寻址数组的数组索引操作。作为对可寻址要求的例外,x也可以是一个(可能带括号的)复合字面量。

因此,你可以使用复合字面量来解决这个问题:

package main

import (
	"fmt"
)

func main() {
	s := &"text"
	fmt.Printf("value: %v, type: %T\n", &s, &s)
	fmt.Printf("value: %v, type: %T\n", &[]string{"literal"}[0], &[]string{"literal"}[0])
}

尽管这是可能的,但我不建议使用这种方法。这不是一个清晰的代码示例。

英文:

The address operation &x can be used with addressable values.
According to the language specification:

> The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.

So, you can work around this using a composite literal:

package main

import (
	"fmt"
)

func main() {
	s := "text"
	fmt.Printf("value: %v, type: %T\n", &s, &s)
	fmt.Printf("value: %v, type: %T\n", &[]string{"literal"}[0], &[]string{"literal"}[0])
}

Even though it's possible I don't recommend using this. This is not an example of clear code.

huangapple
  • 本文由 发表于 2022年8月4日 18:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/73234017.html
匿名

发表评论

匿名网友

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

确定