0

code:

type String struct {
    Result string
}

func main() {
    result := &String{Result:"value"}
    //var test string= "value"
    //result := &test

    testDataBase(result)
    fmt.Print(result.Result) //expect:"34",but:"value"
}

func testDataBase(str interface{}) {
    strV,ok := str.(String)
    if ok {
        strV.Result="34"
    }
}

so,how can I get the result :34 ?

1 Answer 1

1

Use strV, ok := str.(*String),
Like this working sample code:

package main

import "fmt"

type String struct{ Result string }

func main() {
    result := &String{Result: "value"}    
    testDataBase(result)
    fmt.Println(result.Result)
}

func testDataBase(str interface{}) {
    strV, ok := str.(*String)
    if !ok {
        panic("error")
    }
    strV.Result = "34"
}

output:

34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.