0

I am passing a pointer to a string, to a method which takes an interface (I have multiple versions of the method, with different receivers, so I am trying to work with empty interfaces, so that I don't end up with a ton of boilerplate madness. Essentially, I want to populate the string with the first value in the slice. I am able to see the value get populated inside the function, but then for some reason, in my application which calls it, tha value doesn't change. I suspect this is some kind of pointer arithmetic problem, but could really use some help!

I have the following interface :

type HeadInterface interface{
    Head(interface{})
}

And then I have the following functions :

func Head(slice HeadInterface, result interface{}){
    slice.Head(result)
}

func (slice StringSlice) Head(result interface{}){
    result = reflect.ValueOf(slice[0])
    fmt.Println(result)
}

and... here is my call to the function from an application which calls the mehtod...

func main(){
    test := x.StringSlice{"Phil", "Jessica", "Andrea"}

    // empty result string for population within the function
    var result string = ""

    // Calling the function (it is a call to 'x.Head' because I lazily just called th import 'x')
    x.Head(test, &result)

    // I would have thought I would have gotten "Phil" here, but instead, it is still empty, despite the Println in the function, calling it "phil.
    fmt.Println(result)
}

*NOTE : I am aware that getting the first element doesn't need to be this complicated, and could be slice[0] as a straight assertion, but this is more of an exercise in reusable code, and also in trying to get a grasp of pointers, so please don't point out that solution - I would get much more use out of a solution to my actual problem here * :)

2
  • i dont understand how does this code even compile x.Head function expects only one argument which is result but you are passing 2
    – nikoss
    Aug 24, 2018 at 23:55
  • It expects two though... I think you are reading the interface and seeing that. The function which is actually head takes the type we then call with the one parameter that is needed for the version specific to that type. Aug 25, 2018 at 0:26

1 Answer 1

0

As you said in your NOTE, I'm pretty sure this doesn't have to be this complicated, but to make it work in your context:

package main

import (
    "fmt"
    "reflect"
)

type HeadInterface interface {
    Head(interface{})
}

func Head(slice HeadInterface, result interface{}) {
    slice.Head(result)
}

type StringSlice []string

func (slice StringSlice) Head(result interface{}) {
    switch result := result.(type) {
    case *string:
        *result = reflect.ValueOf(slice[0]).String()
        fmt.Println("inside Head:", *result)
    default:
        panic("can't handle this type!")
    }

}

func main() {
    test := StringSlice{"Phil", "Jessica", "Andrea"}

    // empty result string for population within the function
    var result string = ""

    // Calling the function (it is a call to 'x.Head' because I lazily just called th import 'x')
    Head(test, &result)

    // I would have thought I would have gotten "Phil" here, but instead, it is still empty, despite the Println in the function, calling it "phil.
    fmt.Println("outside:", result)
}

The hard part about working with interface{} is that it's hard to be specific about a type's behavior given that interface{} is the most un-specific type. To modify a variable that you pass as a pointer to a function, you have to use the asterisk (dereference) (for example *result) on the variable in order to change the value it points to, not the pointer itself. But to use the asterisk, you have to know it's actually a pointer (something interface{} doesn't tell you) so that's why I used the type switch to be sure it's a pointer to a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.