http://play.golang.org/p/wU44VOa_uP
Here's a simple example:
package main
import "fmt"
type Holder struct {
i int
}
func (h Holder) SetMember(i1 int) {
fmt.Println(i1)
h.i = i1
}
func (h Holder) GetMember() int {
fmt.Println(h.i)
return h.i
}
func main() {
c := Holder{i:3}
c.SetMember(5)
c.GetMember()
}
I'm probably missing a very basic truth about golang.
Why doesn't the value 5 stick after setting it?
This example works:
http://play.golang.org/p/j8hdKWPdsc
package main
import "fmt"
type Holder struct {
i int
}
func (h *Holder) SetMember(i1 int) {
fmt.Println(i1)
h.i = i1
}
func (h *Holder) GetMember() int {
fmt.Println(h.i)
return h.i
}
func main() {
c := &Holder{i:3}
c.SetMember(5)
c.GetMember()
}
In the second example you passing the struct object itself, you are a pointer receiver. You see the change because you have actually modified the original object. In the first example you are passing the copy of the object that contains the same values, you are using a value receiver. You don't see the change because your original object was not changed.
To convince yourself, you can print the addresses of the objects inside main and your SetMember method.
When using pointer receiver:
In main:
fmt.Printf("address in main: %p\n", c)
In SetMember (should be the same as in main):
fmt.Printf("address in SetMember: %p\n", h)
When using value receiver:
In main:
fmt.Printf("address in main: %p\n", &c)
In SetMember (should be different from what is in main) :
fmt.Printf("address in SetMember: %p\n", &h)
Related
package main
import "fmt"
func doStuff(q interface{}) {
*q = MyStruct{2}
}
type MyStruct struct {
f1 int
}
func main() {
ms := MyStruct{1}
doStuff(&ms)
fmt.Printf("Hello, playground: %v\n", ms)
}
Is it possible to set ms through pointer q to have new value MyStruct{2}? I am getting this error invalid indirect of q (type interface {})
interface{} is not a pointer type. So, you cannot change it's underlying value.
Instead, you can modify q value and return it from the doStuff function if you really need to do so.
example:
package main
import "fmt"
func doStuff(q interface{}) interface{} {
q = &MyStruct{2}
return q
}
type MyStruct struct {
f1 int
}
func main() {
var ms interface{} = MyStruct{1}
ms = doStuff(&ms)
fmt.Printf("Hello, playground: %v\n", ms)
}
You can't using the interface{} type, since it's not a literal type you can memory address like you're attempting. An interface type describes general behavior, not a specific value.
If you use the MyStruct type directly, you can get a pointer and do what you want.
package main
import "fmt"
func doStuff(q *MyStruct) {
*q = MyStruct{2}
}
type MyStruct struct {
f1 int
}
func main() {
ms := MyStruct{1}
doStuff(&ms)
fmt.Printf("Hello, playground: %v\n", ms)
}
Go playground linke
Is there anyway to make a map of function pointers, but functions that take recievers? I know how to do it with regular functions:
package main
func someFunc(x int) int {
return x
}
func main() {
m := make(map[string]func(int)int, 0)
m["1"] = someFunc
print(m["1"](56))
}
But can you do that with functions that take recievers? Something like this (though I've tried this and it doesn't work):
package main
type someStruct struct {
x int
}
func (s someStruct) someFunc() int {
return s.x
}
func main() {
m := make(map[string](someStruct)func()int, 0)
s := someStruct{56}
m["1"] = someFunc
print(s.m["1"]())
}
An obvious work around is to just pass the struct as a parameter, but that's a little dirtier than I would have liked
You can do that using Method Expressions:
https://golang.org/ref/spec#Method_expressions
The call is a bit different, since the method expression takes the receiver as the first argument.
Here's your example modified:
package main
type someStruct struct {
x int
}
func (s someStruct) someFunc() int {
return s.x
}
func main() {
m := make(map[string]func(someStruct)int, 0)
s := someStruct{56}
m["1"] = (someStruct).someFunc
print(m["1"](s))
}
And here's a Go playground for you to test it:
https://play.golang.org/p/PLi5A9of-U
I am fairly new to Go and I have been doing OOP for a long time. Now, I understand that inheritance is done via composition but ... I'd like to send a specialization in a function expecting a generalization as such:
package main
import (
"fmt"
)
type A struct {
ONE int
}
type B struct {
A
ANOTHER int
}
func main() {
fmt.Println("Hello, playground")
a := A{1}
b := B{a, 2}
fmt.Println(a.ONE)
fmt.Println(b.ANOTHER)
takeA(&b)
}
func takeA(a *A) {
fmt.Println(a.ONE)
}
Sure, I can do takeA(&b.A) but it defeats the hierarchy I am trying to set up as some A fields/method could be shadowed in B.
Is there a way around this? Or should I create a dumb interface such as:
package main
import (
"fmt"
)
type A struct {
ONE int
}
func (a A) getOne() int{
return a.ONE
}
type B struct {
A
ANOTHER int
}
type HasOne interface {
getOne() int
}
func main() {
fmt.Println("Hello, playground")
a := A{1}
b := B{a, 2}
fmt.Println(a.ONE)
fmt.Println(b.ANOTHER)
takeA(b)
}
func takeA(a HasOne) {
fmt.Println(a.getOne())
}
There is no inheritance in Go. Composition doesn't give you dynamic dispatch (which is usually implied when 'polymorphism' and 'inheritance' are used in the same paragraph): the embedded method call always happens on the embedded field as a receiver, not on the container type it is embedded into.
I was expecting to see 3, what's going on?
package main
import "fmt"
type Counter struct {
count int
}
func (self Counter) currentValue() int {
return self.count
}
func (self Counter) increment() {
self.count++
}
func main() {
counter := Counter{1}
counter.increment()
counter.increment()
fmt.Printf("current value %d", counter.currentValue())
}
http://play.golang.org/p/r3csfrD53A
Your method receiver is a struct value, which means the receiver gets a copy of the struct when invoked, therefore it's incrementing the copy and your original isn't updated.
To see the updates, put your method on a struct pointer instead.
func (self *Counter) increment() {
self.count++
}
Now self is a pointer to your counter variable, and so it'll update its value.
http://play.golang.org/p/h5dJ3e5YBC
I want to add to #user1106925 response.
If you need to use the custom type inside a map you need to use a map to pointer. because the for l,v :=range yourMapType will receive a copy of the struct.
Here a sample:
package main
import "fmt"
type Counter struct {
Count int
}
func (s *Counter) Increment() {
s.Count++
}
func main() {
// Using map to type
m := map[string]Counter{
"A": Counter{},
}
for _, v := range m {
v.Increment()
}
fmt.Printf("A: %v\n", m["A"].Count)
// Now using map to pointer
mp := map[string]*Counter{
"B": &Counter{},
}
for _, v := range mp {
v.Increment()
}
fmt.Printf("B: %v\n", mp["B"].Count)
}
The output is:
$ go build && ./gotest
A: 0
B: 1
If I have a struct like this
type myStruct struct {
mystring string
myint int
}
and if I have a function that returns a new myStruct like this
func New() myStruct {
s := myStruct{}
s.mystring = "string"
s.myint = 1
return s
}
Because I first store it in the "s" variable before returning it, is my function actually making 2 myStruct values instead of one?
And if so, is it then a better practice to make sure I don't first store it in the variable?
The return statement will return a copy of the myStruct object value. If it is a small object then this is fine.
If you intend for the caller to be able to modify this object, and the struct will have methods that use a pointer as the receiver, then it makes more sense to return a pointer to your struct instead:
func New() *myStruct {
s := myStruct{}
s.mystring = "string"
s.myint = 1
return &s
}
You can see the copy happening when you compare the memory address of value vs pointer return types: http://play.golang.org/p/sj6mivYSHg
package main
import (
"fmt"
)
type myStruct struct {
mystring string
myint int
}
func NewObj() myStruct {
s := myStruct{}
fmt.Printf("%p\n", &s)
return s
}
func NewPtr() *myStruct {
s := &myStruct{}
fmt.Printf("%p\n",s)
return s
}
func main() {
o := NewObj()
fmt.Printf("%p\n",&o)
p := NewPtr()
fmt.Printf("%p\n",p)
}
0xf8400235a0 // obj inside NewObj()
0xf840023580 // obj returned to caller
0xf840023640 // ptr inside of NewPtr()
0xf840023640 // ptr returned to caller
I'm definitely not a Go expert (or even novice :) ), but as #max.haredoom mentioned, you can allocate variables in the function signature itself. In that way, you can also omit the s in the return:
package main
import "fmt"
type myStruct struct {
mystring string
myint int
}
func New() (s myStruct) {
s.mystring = "string"
s.myint = 1
return
}
func main() {
r := New()
fmt.Println(r)
}
// Outputs {string 1}
In the examples that I have come across in Effective Go, it does seem to be the most common way of doing things of this nature, but again, I am definitely not an authority on the subject (and will look for additional info on the actual performance).
I think I found the answer by using defer.
I updated the function so that there's a deferred modification to the myStruct value. This means it will happen after the return, but before it is received on the other end.
When I do this, the struct that is received by the caller does not show the updated value, so it appears as though I am indeed returning a copy.
func New() myStruct {
s := myStruct{}
defer func() {
s.mystring = "new value" // defer an update to the value
}()
s.mystring = "string"
s.myint = 1
return s
}
func main() {
b := New()
fmt.Println(b) // still shows the original value
}
http://play.golang.org/p/WWQi8HpDny