Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a 'string pointer' to a string in Golang?

Is it possible to get the string value from a pointer to a string?

I am using the goopt package to handle flag parsing and the package returns *string only. I want to use these values to call a function in a map.

Example

var strPointer = new(string) *strPointer = "string"  functions := map[string]func() {     "string": func(){         fmt.Println("works")     }, }    //Do something to get the string value  functions[strPointerValue]() 

returns

./prog.go:17:14: cannot use strPointer (type *string)  as type string in map index 
like image 627
Jared Meyering Avatar asked Oct 21 '14 18:10

Jared Meyering


People also ask

Is string a pointer in Go?

A string in Go is a value. Thus, a string cannot be nil . However, a pointer to a string (or *string ) can be nil .

Can a string be null in Go?

Types in zero are treated like zero values in Go: blank string input will produce a null zero. String , and null Strings will JSON encode to "" . Zero values of these types will be considered null to SQL.

How do I declare a pointer variable in Golang?

You can also use the shorthand (:=) syntax to declare and initialize the pointer variables. The compiler will internally determine the variable is a pointer variable if we are passing the address of the variable using &(address) operator to it. Example: Go.

Is string a reference type in Golang?

String is a reference type, not a value type.

Should I use pointers or normal strings in Golang?

A good rule of thumb is to use normal strings unless you need nil. Normal strings are easier and safer to use in Go. Pointers require you to write more code because you need to check that a *string has a value before dereferencing. An empty string value "" and nil are not the same thing.

How do you initialize a pointer in Golang?

Pointers Initialization in Go The pointers of a type are initialized using the address-of (&) operator on a subject of that specific type. Here is the way to do it. 4. Go Pointers dereferencing Dereferencing a pointer means getting the value inside the address the pointer holds.

How to convert a slice to string in Golang?

Golang string Join () is an inbuilt function that converts slice to string. Python slice contains string data. With strings.Join () function, we can convert a string slice to a string. We will take a slice of strings and convert slices to strings. Transform a string slice into a string.

How to create string literals in Golang?

Using double quotes (“”): Here, the string literals are created using double-quotes (“”). This type of string support escape character as shown in the below table, but does not span multiple lines. This type of string literals is widely used in Golang programs. Single quote (‘). It is only allowed inside character literals Double quote (“).


1 Answers

Dereference the pointer:

strPointerValue := *strPointer 
like image 62
OneOfOne Avatar answered Sep 19 '22 23:09

OneOfOne