Reputation: 3
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 ?
Upvotes: 0
Views: 106
Reputation:
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
Upvotes: 1