-2

I'm new to golang and am struggling to wrap my head around how to get a pointer to a concatenated string without a helper function, and what the reasoning behind that is. The same goes for type bool.

For example, I cannot do either of the below:

myBool := &true
myString := &string("foo" + someVar + "bar")

As a quick/dirty workaround I wrote helper functions that accept a bool or a string and return a pointer.

For example:

func GetBoolPointer(i bool) *bool {
    return &i
}

It's especially odd to me because I can directly get a pointer for other types, like myVar := &SomeDefinedType.

user797963
  • 2,907
  • 9
  • 47
  • 88
  • 1
    First create a var for the string then take the address var myString = "hi"; var ps *string = &myString – Jerinaw Apr 08 '21 at 03:30
  • If this question is reference to using the AWS SDK (as are some previous questions on this topic), then use the the [AWS Value and Pointer Conversion Utilities](https://pkg.go.dev/github.com/aws/aws-sdk-go/aws#hdr-Value_and_Pointer_Conversion_Utilities). – Charlie Tumahai Apr 08 '21 at 04:12
  • 1
    Being unable to take the address of a literal `true` is as odd as being unable to take the address of a literal `17`: Not at all. Only variables have addresses. Duplicate. – Volker Apr 08 '21 at 04:36
  • 1
    @CeriseLimón - no, it's not referencing the AWS SDK, though interestingly enough, it is another cloud providers API that does not have conversion utilities. – user797963 Apr 08 '21 at 14:24

1 Answers1

2

You can only get the address of an addressable value:

https://golang.org/ref/spec#Address_operators

Literals are not addressable. You need to have a variable holding that value:

t:=true
myBool:=&t
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59