How to set any value to interface{} - go

I have following code:
package main
import (
"fmt"
)
type Point struct {
x,y int
}
func decode(value interface{}) {
fmt.Println(value) // -> &{0,0}
// This is simplified example, instead of value of Point type, there
// can be value of any type.
value = &Point{10,10}
}
func main() {
var p = new(Point)
decode(p)
fmt.Printf("x=%d, y=%d", p.x, p.y) // -> x=0, y=0, expected x=10, y=10
}
I want to set value of any type to the value passed to decode function. Is it possible in Go, or I misunderstand something?
http://play.golang.org/p/AjZHW54vEa

Generically, only using reflection:
package main
import (
"fmt"
"reflect"
)
type Point struct {
x, y int
}
func decode(value interface{}) {
v := reflect.ValueOf(value)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
n := reflect.ValueOf(Point{10, 10})
v.Set(n)
}
func main() {
var p = new(Point)
decode(p)
fmt.Printf("x=%d, y=%d", p.x, p.y)
}

I'm not sure of your exact goal.
If you want to assert that value is a pointer to Point and change it, you can do that :
func decode(value interface{}) {
p := value.(*Point)
p.x=10
p.y=10
}

Related

Map implementation - Code smell in Lookup() & Insert()

Below code is Map implementation using array, slice & pointers:
package gomap
import "fmt"
type Entry struct {
stockName string
stockValue float64
}
type bucket []Entry
type Map [4]bucket
type bucketElementPosition [4]int
var emptyPosition = bucketElementPosition{0, 0, 0, 0}
func Newmap() Map {
return Map{} // [(nil,0,0), (nil,0,0), (nil,0,0), (nil,0,0)]
}
func (m *Map) Lookup(key string) float64 {
bucketNumber := (key[0] - 'A') / 7
for _, entry := range m[bucketNumber] { // (*m)[bucketNumber]
if entry.stockName == key {
return entry.stockValue
}
}
return 0
}
func (m *Map) Insert(key string, value float64) {
bucketNumber := (key[0] - 'A') / 7
if cap(m[bucketNumber]) == 0 { // (*m)[bucketNumber]
fmt.Println("bucket number:", bucketNumber)
m[bucketNumber] = make([]Entry, 0, 100)
}
m[bucketNumber] = append(m[bucketNumber], Entry{key, value})
emptyPosition[bucketNumber]++
}
package main
import (
"fmt"
"github.com/myhub/cs61a/gomap"
)
func main() {
var stockData = gomap.Newmap()
(&stockData).Insert("AAPL", 94.56)
(&stockData).Insert("HSCO", 26.72)
(&stockData).Insert("NMZN", 697.45)
(&stockData).Insert("ZSFT", 50.81)
(&stockData).Insert("PMGN", 150.83)
fmt.Println((&stockData).Lookup("AAPL"))
fmt.Println((&stockData).Lookup("HSCO"))
fmt.Println((&stockData).Lookup("NMZN"))
fmt.Println((&stockData).Lookup("ZSFT"))
fmt.Println((&stockData).Lookup("PMGN"))
}
Code smell bucketNumber := (key[0] - 'A') / 7 can be taken care by reading bits and hash.
but for line type bucket []Entry
How to make the concrete type Map, Entry type agnostic? because key is fixed as string and value is fixed as float64
I completed the home assignment like this:
package gomap
import "fmt"
type Entry struct {
key, value interface{}
}
type Map struct {
buckets [4][]Entry
}
func Newmap() *Map {
return &Map{}
}
func (m *Map) bucketNumber(key interface{}) int {
s := fmt.Sprint(key)
return int(s[0]) % len(m.buckets)
}
func (m *Map) Lookup(key interface{}) interface{} {
bucketNumber := m.bucketNumber(key)
for _, entry := range m.buckets[bucketNumber] {
if entry.key == key {
return entry.value
}
}
return nil
}
func (m *Map) Insert(key interface{}, value interface{}) {
bucketNumber := m.bucketNumber(key)
m.buckets[bucketNumber] = append(m.buckets[bucketNumber], Entry{key, value})
}
https://play.golang.org/p/KmVSeDavc3y
There are a few ways you can do that. The most obvious is to use interface{} for key and value. However, there's a type-safe way of doing the keys. You can make Key an interface:
type Key interface {
// Returns the bucket number for the key
BucketNumber() int
// Eq compares the key with the given key
Eq(Key) bool
}
Then you can have an Entry using this key:
type Entry struct {
k Key
v interface{}
}
You can rewrite the map functions to use this key. Value would have to be an interface, and you have to use type assertions to get the underlying value.

Go use string var to fill struct [duplicate]

Here is a simple go program that is not working :
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func getProperty(v *Vertex, property string) (string) {
return v[property]
}
Error:
prog.go:18: invalid operation: v[property] (index of type *Vertex)
What I want is to access the Vertex X property using its name. If I do v.X it works, but v["X"] doesn't.
Can someone tell me how to make this work ?
Most code shouldn't need this sort of dynamic lookup. It's inefficient compared to direct access (the compiler knows the offset of the X field in a Vertex structure, it can compile v.X to a single machine instruction, whereas a dynamic lookup will need some sort of hash table implementation or similar). It's also inhibits static typing: the compiler has no way to check that you're not trying to access unknown fields dynamically, and it can't know what the resulting type should be.
But... the language provides a reflect module for the rare times you need this.
package main
import "fmt"
import "reflect"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getField(&v, "X"))
}
func getField(v *Vertex, field string) int {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return int(f.Int())
}
There's no error checking here, so you'll get a panic if you ask for a field that doesn't exist, or the field isn't of type int. Check the documentation for reflect for more details.
You now have the project oleiade/reflections which allows you to get/set fields on struct value or pointers.
It makes using the reflect package less tricky.
s := MyStruct {
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
fieldsToExtract := []string{"FirstField", "ThirdField"}
for _, fieldName := range fieldsToExtract {
value, err := reflections.GetField(s, fieldName)
DoWhatEverWithThatValue(value)
}
// In order to be able to set the structure's values,
// a pointer to it has to be passed to it.
_ := reflections.SetField(&s, "FirstField", "new value")
// If you try to set a field's value using the wrong type,
// an error will be returned
err := reflection.SetField(&s, "FirstField", 123) // err != nil
With getAttr, you can get and set easy.
package main
import (
"fmt"
"reflect"
)
func getAttr(obj interface{}, fieldName string) reflect.Value {
pointToStruct := reflect.ValueOf(obj) // addressable
curStruct := pointToStruct.Elem()
if curStruct.Kind() != reflect.Struct {
panic("not struct")
}
curField := curStruct.FieldByName(fieldName) // type: reflect.Value
if !curField.IsValid() {
panic("not found:" + fieldName)
}
return curField
}
func main() {
type Point struct {
X int
y int // Set prefix to lowercase if you want to protect it.
Z string
}
p := Point{3, 5, "Z"}
pX := getAttr(&p, "X")
// Get test (int)
fmt.Println(pX.Int()) // 3
// Set test
pX.SetInt(30)
fmt.Println(p.X) // 30
// test string
getAttr(&p, "Z").SetString("Z123")
fmt.Println(p.Z) // Z123
py := getAttr(&p, "y")
if py.CanSet() { // The necessary condition for CanSet to return true is that the attribute of the struct must have an uppercase prefix
py.SetInt(50) // It will not execute here because CanSet return false.
}
fmt.Println(p.y) // 5
}
Run it on 👉
Reference
CanSet
A Good example of the reflex: https://stackoverflow.com/a/6396678
You can marshal the struct and unmarshal it back to map[string]interface{}. But, it would convert all the number values to float64 so you would have to convert it to int manually.
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func getProperty(v *Vertex, property string) float64 {
m, _ := json.Marshal(v)
var x map[string]interface{}
_ = json.Unmarshal(m, &x)
return x[property].(float64)
}
I want to offer a different approach that is not using reflection:
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
type Getter func(v *Vertex) int
var VertexAccess = map[string]Getter{
"X": func(v *Vertex) int { return v.X },
"Y": func(v *Vertex) int { return v.Y },
}
func getProperty(v *Vertex, property string) int {
return VertexAccess[property](v)
}
https://go.dev/play/p/2E7LZBWx7yZ
This is an O(1) map lookup and a function call which should perform better than reflection. Obviously, you need some scaffolding code for every type you want to support. On the plus side, you can refactor your code easily; your function getProperty is a potential anti-pattern to https://martinfowler.com/bliki/TellDontAsk.html

Access struct property by name

Here is a simple go program that is not working :
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func getProperty(v *Vertex, property string) (string) {
return v[property]
}
Error:
prog.go:18: invalid operation: v[property] (index of type *Vertex)
What I want is to access the Vertex X property using its name. If I do v.X it works, but v["X"] doesn't.
Can someone tell me how to make this work ?
Most code shouldn't need this sort of dynamic lookup. It's inefficient compared to direct access (the compiler knows the offset of the X field in a Vertex structure, it can compile v.X to a single machine instruction, whereas a dynamic lookup will need some sort of hash table implementation or similar). It's also inhibits static typing: the compiler has no way to check that you're not trying to access unknown fields dynamically, and it can't know what the resulting type should be.
But... the language provides a reflect module for the rare times you need this.
package main
import "fmt"
import "reflect"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getField(&v, "X"))
}
func getField(v *Vertex, field string) int {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return int(f.Int())
}
There's no error checking here, so you'll get a panic if you ask for a field that doesn't exist, or the field isn't of type int. Check the documentation for reflect for more details.
You now have the project oleiade/reflections which allows you to get/set fields on struct value or pointers.
It makes using the reflect package less tricky.
s := MyStruct {
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
fieldsToExtract := []string{"FirstField", "ThirdField"}
for _, fieldName := range fieldsToExtract {
value, err := reflections.GetField(s, fieldName)
DoWhatEverWithThatValue(value)
}
// In order to be able to set the structure's values,
// a pointer to it has to be passed to it.
_ := reflections.SetField(&s, "FirstField", "new value")
// If you try to set a field's value using the wrong type,
// an error will be returned
err := reflection.SetField(&s, "FirstField", 123) // err != nil
With getAttr, you can get and set easy.
package main
import (
"fmt"
"reflect"
)
func getAttr(obj interface{}, fieldName string) reflect.Value {
pointToStruct := reflect.ValueOf(obj) // addressable
curStruct := pointToStruct.Elem()
if curStruct.Kind() != reflect.Struct {
panic("not struct")
}
curField := curStruct.FieldByName(fieldName) // type: reflect.Value
if !curField.IsValid() {
panic("not found:" + fieldName)
}
return curField
}
func main() {
type Point struct {
X int
y int // Set prefix to lowercase if you want to protect it.
Z string
}
p := Point{3, 5, "Z"}
pX := getAttr(&p, "X")
// Get test (int)
fmt.Println(pX.Int()) // 3
// Set test
pX.SetInt(30)
fmt.Println(p.X) // 30
// test string
getAttr(&p, "Z").SetString("Z123")
fmt.Println(p.Z) // Z123
py := getAttr(&p, "y")
if py.CanSet() { // The necessary condition for CanSet to return true is that the attribute of the struct must have an uppercase prefix
py.SetInt(50) // It will not execute here because CanSet return false.
}
fmt.Println(p.y) // 5
}
Run it on 👉
Reference
CanSet
A Good example of the reflex: https://stackoverflow.com/a/6396678
You can marshal the struct and unmarshal it back to map[string]interface{}. But, it would convert all the number values to float64 so you would have to convert it to int manually.
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func getProperty(v *Vertex, property string) float64 {
m, _ := json.Marshal(v)
var x map[string]interface{}
_ = json.Unmarshal(m, &x)
return x[property].(float64)
}
I want to offer a different approach that is not using reflection:
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
type Getter func(v *Vertex) int
var VertexAccess = map[string]Getter{
"X": func(v *Vertex) int { return v.X },
"Y": func(v *Vertex) int { return v.Y },
}
func getProperty(v *Vertex, property string) int {
return VertexAccess[property](v)
}
https://go.dev/play/p/2E7LZBWx7yZ
This is an O(1) map lookup and a function call which should perform better than reflection. Obviously, you need some scaffolding code for every type you want to support. On the plus side, you can refactor your code easily; your function getProperty is a potential anti-pattern to https://martinfowler.com/bliki/TellDontAsk.html

Alternative for function overloading in Go?

Is it possible to work similar way like the function overloading or optional parameter in C# using Golang? Or maybe an alternative way?
The idiomatic answer to optional parameters in Go is wrapper functions:
func do(a, b, c int) {
// ...
}
func doSimply(a, b) {
do(a, b, 42)
}
Function overloading was intentionally left out, because it makes code hard(er) to read.
Neither function overloading nor optional arguments are directly supported. You could work around them building your own arguments struct. I mean like this (untested, may not work...) EDIT: now tested...
package main
import "fmt"
func main() {
args:=NewMyArgs("a","b") // filename is by default "c"
args.SetFileName("k")
ret := Compresser(args)
fmt.Println(ret)
}
func Compresser(args *MyArgs) string {
return args.dstFilePath + args.srcFilePath + args.fileName
}
// a struct with your arguments
type MyArgs struct
{
dstFilePath, srcFilePath, fileName string
}
// a "constructor" func that gives default values to args
func NewMyArgs(dstFilePath string, srcFilePath string) *MyArgs {
return &MyArgs{
dstFilePath: dstFilePath,
srcFilePath:srcFilePath,
fileName :"c"}
}
func (a *MyArgs) SetFileName(value string){
a.fileName=value;
}
There are some hints here using variadic arguments, for example:
sm1 := Sum(1, 2, 3, 4) // = 1 + 2 + 3 + 4 = 10
sm2 := Sum(1, 2) // = 1 + 2 = 3
sm3 := Sum(7, 1, -2, 0, 18) // = 7 + 1 + -2 + 0 + 18 = 24
sm4 := Sum() // = 0
func Sum(numbers ...int) int {
n := 0
for _,number := range numbers {
n += number
}
return n
}
Or ...interface{} for any types:
Ul("apple", 7.2, "BANANA", 5, "cHeRy")
func Ul(things ...interface{}) {
fmt.Println("<ul>")
for _,it := range things {
fmt.Printf(" <li>%v</li>\n", it)
}
fmt.Println("</ul>")
}
An approach I use sometime for constructing an object using New methods having different arguments is to have a "flavor" pseudo type. You can try it on the Go Playground https://play.golang.org/p/5To5AcY-MRe
package main
import "fmt"
type flavorA struct{}
type flavorB struct{}
var FlavorA = flavorA{}
var FlavorB = flavorB{}
type Something struct {
i int
f float64
}
func (flavor flavorA) NewSomething(i int) *Something {
return &Something{i:i, f:0.0}
}
func (flavor flavorB) NewSomething(f float64) *Something {
return &Something{i:0, f:f}
}
func main() {
fmt.Println(FlavorA.NewSomething(1), FlavorB.NewSomething(2))
}
When you have many arguments it may make sense to use a new struct for them or to define a new MyOptionBuilder type to build and store all the arguments and to construct nice defaults.
Here's a simple example where the go defaults for types are okay.
package main
import "fmt"
type FancyFuncOptions struct {
I int64
S string
F float64
//...many more...
}
func FancyFunc(opts *FancyFuncOptions) {
fmt.Println("%v", opts)
}
func main() {
// simple way
options := &FancyFuncOptions{S: "happy"}
FancyFunc(options)
In golang you'll see people using method-chaining for this, if the options have complex logic.
package main
import "fmt"
type FancyFuncOptions struct {
I int64
S string
F float64
//...many more...
}
// chaining style
func NewFancyFuncOptions() *FancyFuncOptions {
return &FancyFuncOptions{I: 100, S: "empty", F: 0.1}
}
func (o *FancyFuncOptions) SetI(i int64) *FancyFuncOptions {
o.I = i
return o
}
func (o *FancyFuncOptions) SetS(s string) *FancyFuncOptions {
o.S = s
return o
}
func FancyFunc(opts *FancyFuncOptions) {
fmt.Println("%v", opts)
}
func main() {
// fancier
options = NewFancyFuncOptions().SetI(234).SetS("happy")
FancyFunc(options)
(https://go.dev/play/p/Ae_6Y6kZa97)
Make sense?

How do I put a vector inside of a struct in Go?

I'm trying to put a vector variable inside a struct in Google's Go programming language. This is what I have so far:
Want:
type Point struct { x, y int }
type myStruct struct {
myVectorInsideStruct vector;
}
func main(){
myMyStruct := myStruct{vector.New(0)};
myPoint := Point{2,3};
myMyStruct.myVectorInsideStruct.Push(myPoint);
}
Have:
type Point struct { x, y int }
func main(){
myVector := vector.New(0);
myPoint := Point{2,3};
myVector.Push(myPoint);
}
I can get the vector to work in my main function just fine, but I want to encapsulate it inside a struct for easier use.
I'm not sure whether this is what you want, so leave a comment if it doesn't work:
package main
import "container/vector";
type Point struct { x, y int };
type mystruct struct {
myVectorInsideStruct * vector.Vector;
}
func main() {
var myMyStruct mystruct;
myMyStruct.myVectorInsideStruct = new(vector.Vector);
myPoint := Point{2,3};
myMyStruct.myVectorInsideStruct.Push(myPoint);
}
Not sure this is what you want, but:
package main
import (
"fmt";
"container/vector";
)
type myStruct (
struct {
myVectorInsideStruct vector.IntVector;
}
)
func main() {
v := new(myStruct);
v.myVectorInsideStruct.Init(0);
for i := 1 ; i < 10 ; i++ {
v.myVectorInsideStruct.Push(i);
}
fmt.Printf("v.myVectorInsideStruct: %v\n", v.myVectorInsideStruct.Data());
}

Resources