Go: edit in place of map values - dictionary

I wrote a simple program using the Go Playground at golang.org.
The output is obviously:
second test
first test
Is there a way to edit the map value in place? I know I can't take the andress of a.Things[key]. So, is setting a.Things[key] = firstTest the only way to do it? Maybe with a function ChangeThing(key string, value string)?

You could do it by making the values of your map pointers to another struct.
http://play.golang.org/p/UouwDGuVpi
package main
import "fmt"
type A struct {
Things map[string]*str
}
type str struct {
s string
}
func (a A) ThingWithKey(key string) *str {
return a.Things[key]
}
func main() {
variable := A{}
variable.Things = make(map[string]*str)
variable.Things["first"] = &str{s:"first test"}
firstTest := variable.ThingWithKey("first")
firstTest.s = "second test"
fmt.Println(firstTest.s)
fmt.Println(variable.ThingWithKey("first").s)
}

You can use a pointer as the map value http://play.golang.org/p/BCsmhevGMX

Related

Dynamic argument and Generic type

I have following code:
package main
import (
"fmt"
)
func test(data interface{}) {
data = "123"
}
func main() {
t := "org"
test(&t)
fmt.Println(t)
e := 1
test(&e)
fmt.Println(e)
}
I tried to add pointer to *interface{} but it throws errors, how can I assign string "test" to t when I print it? right now t will be printed out as "org"
I am asking because I am not sure how I can use dynamic type here, for instance, josn.Unmarshal(data []byte, v interface{}) I used this function, and it can convert databyte to any type we want as long as we pass the reference.
I think you are expecting interface to be much more magic than it actually is.
Consider this variant of your program:
package main
import (
"fmt"
)
func test(p *string) {
s := "test"
p = &s
}
func main() {
t := "org"
test(&t)
fmt.Println(t)
}
Do you expect this to print test? If so, we have a bigger problem. :-) If not, why do you expect the version with p interface{} to change t when assigning directly to p?
If we change test to write through *p:
func test(p *string) {
*p = "test"
}
the program does print test, as you expected.
All that remains now is to handle the case when p is declared instead as data interface{}. As in bserdar's answer, you must first extract the underlying *string pointer from the interface object in data. You can then use that pointer to set main's variable t. You could do this with two steps:
func test(data interface{}) {
p := data.(*string)
*p = "test"
}
for instance, or you can do it all in one line.
You are setting the interface, not the underlying value. Instead you should do
func test(data interface{}){
*data.(*string) = "aaa"
}
That is, first get the underlying string pointer, then set the string pointed by it.

Function pointer params changes

I'm trying to write some function that changes a slice of structs using a pointer parameter.
I did some playground with this type of code in GoPlayground and I found out that I have some mistake but I don't know what is the best way to manage it
package main
import "fmt"
type Person struct {
name string
}
func doSomething(person *Person) {
person.name = "John"
}
func main() {
var persons []Person
p := Person{name:"David"}
persons = append(persons, p)
doSomething(&p)
fmt.Println(persons)
}
doSomething isn't change anything in persons, how can I implement something like that?
Thanks a lot!
It changes p, but the value in persons is a copy of p, not a pointer to p (as you can see by printing p: https://play.golang.org/p/4b5fhdtuR8R). If you use a slice of pointers you'll get what you're looking for:
var persons []*Person
p := &Person{name: "David"}
persons = append(persons, p)
doSomething(p)
fmt.Println(persons[0])
fmt.Println(p)
Playground: https://play.golang.org/p/UTO1D5zKA0H

Difference between &Struct{} vs Struct{}

Is there a reason why I should create a struct using &StructName{} instead of Struct{}? I see many examples using the former syntax, even in the Effective Go Page but I really can not understand why.
Additional Notes:
I'm not sure whether I explained my problem well with these two approaches so let me refine my question.
I know that by using the & I will recieve a pointer instead of a value however I would like to know why would I use the &StructName{} instead of the StructName{}. For example, is there any benefits of using:
func NewJob(command string, logger *log.Logger) *Job {
return &Job{command, logger}
}
instead of:
func NewJob(command string, logger *log.Logger) Job {
return Job{command, logger}
}
Well, they will have different behavior. Essentially if you want to modify state using a method on a struct, then you will need a pointer, otherwise a value will be fine. Maybe an example will be better:
package main
import "fmt"
type test_struct struct {
Message string
}
func (t test_struct)Say (){
fmt.Println(t.Message)
}
func (t test_struct)Update(m string){
t.Message = m;
}
func (t * test_struct) SayP(){
fmt.Println(t.Message)
}
func (t* test_struct) UpdateP(m string) {
t.Message = m;
}
func main(){
ts := test_struct{}
ts.Message = "test";
ts.Say()
ts.Update("test2")
ts.Say() // will still output test
tsp := &test_struct{}
tsp.Message = "test"
tsp.SayP();
tsp.UpdateP("test2")
tsp.SayP() // will output test2
}
And you can run it here go playground
Assuming you know the general difference between a pointer and a value:
The first way allocates a struct and assigns a pointer to that allocated struct to the variable p1.
p1 := &StructName{}
The second way allocates a struct and assigns a value (the struct itself) to the variable s.
Then a pointer to that struct may be assigned to another variable (p2 in the following example).
s := StructName{}
p2 := &s

Builtin func "append" in method for pointer receiver

Having two types:
type Headers []HeaderItem
type HeaderItem struct { // This one doesn't really matter. Could be any other type
Name string
Value string
}
I would like to add a function with the slice as receiver. How can I do something like this (pseudo-code):
func (h *Headers) AddHeaderItem(item HeaderItem) {
h = &(append( *h, item ))
}
The compiler complains about it, so this doesn't work.
I tried:
func (h Headers) AddHeaderItem(item HeaderItem) {
h = append( h, item )
}
This actually compiles but doesn't do what I want: when later on rangeing over the items, I get an empty result.
Inside the AddHeaderItem() method h is a pointer. You do not want to change the pointer but the pointed value:
func (h *Headers) AddHeaderItem(item HeaderItem) {
*h = append(*h, item)
}
Testing it:
h := Headers{}
fmt.Println(h)
h.AddHeaderItem(HeaderItem{"myname1", "myvalue1"})
fmt.Println(h)
h.AddHeaderItem(HeaderItem{"myname2", "myvalue2"})
fmt.Println(h)
Output:
[]
[{myname1 myvalue1}]
[{myname1 myvalue1} {myname2 myvalue2}]
Try it on the Go Playground.
Don't do this. Don't try too be too clever. Just use a struct with the slice as a member. It adds literally zero overhead, and whoever will have to look at your code later (including you) will be grateful.

How to create slice of struct using reflection?

I need to create a slice of struct from its interface with reflection.
I used Reflection because do not see any other solution without using it.
Briefly, the function receives variadic values of Interface.
Then, with reflection creates slice and passes it into another function.
Reflection asks to type assertion
SliceVal.Interface().(SomeStructType)
But, I cannot use it.
Code in playground http://play.golang.org/p/EcQUfIlkTe
The code:
package main
import (
"fmt"
"reflect"
)
type Model interface {
Hi()
}
type Order struct {
H string
}
func (o Order) Hi() {
fmt.Println("hello")
}
func Full(m []Order) []Order{
o := append(m, Order{H:"Bonjour"}
return o
}
func MakeSlices(models ...Model) {
for _, m := range models {
v := reflect.ValueOf(m)
fmt.Println(v.Type())
sliceType := reflect.SliceOf(v.Type())
emptySlice := reflect.MakeSlice(sliceType, 1, 1)
Full(emptySlice.Interface())
}
}
func main() {
MakeSlices(Order{})
}
You're almost there. The problem is that you don't need to type-assert to the struct type, but to the slice type.
So instead of
SliceVal.Interface().(SomeStructType)
You should do:
SliceVal.Interface().([]SomeStructType)
And in your concrete example - just changing the following line makes your code work:
Full(emptySlice.Interface().([]Order))
Now, if you have many possible models you can do the following:
switch s := emptySlice.Interface().(type) {
case []Order:
Full(s)
case []SomeOtherModel:
FullForOtherModel(s)
// etc
}

Resources