Why Doesn't a Go Function Field Setter Retain the Function? - function

Given this short program:
package main
import "fmt"
type Foo struct {
doer func()
}
func (f Foo) SetDoer(doer func()) {
f.doer = doer
}
func main() {
foo := Foo{func() { fmt.Println("original") }}
foo.doer()
foo.SetDoer(func() { fmt.Println("replacement") })
foo.doer()
}
The output is:
original
original
I had expected it to be:
original
replacement
Why isn't it? Note that the output is as expected if I set foo.doer directly in main(). Just not if I use the SetDoer method.

In Go, the item on the left of the function name is the receiving type. This is the type from which a function can be called. However, receiver can be both pointers or value types. In this case, it is a value. The receiver is purely for the purpose of organization, under the covers, it is passed to the function like any other argument. You're passing by value so a copy of foo is passed into SetDoer, the value is modified, then the setter returns, the value goes out of scope and in the calling scope you're working with the original.
Try this;
// make the receiver a pointer
func (f *Foo) SetDoer(doer func()) {
f.doer = doer
}
// instantiate as pointer
foo := &Foo{func() { fmt.Println("original") }}
foo.SetDoer(func() { fmt.Println("replacement") })
// now the version of doer on foo has been updated.
playground example; https://play.golang.org/p/ZQlvKiluu3

Related

Can functions with interfaces be passed as parameters?

I know that functions can be passed as parameters. But I would like to use a function taking a type implementing an interface as input to a function. Is this still possible?
I have tried the following and it gives cannot use myfn1 (type func(int)) as type fn in argument to test as error.
package main
import "fmt"
type intf interface{}
type fn func(i intf)
func myfn1(i int) {
fmt.Printf("\ni is %v", i)
}
func myfn2(i int) {
fmt.Printf("\ni is %v", i)
}
func test(f fn, val int) {
f(val)
}
func main() {
test(fn(myfn1), 123)
test(myfn2, 321)
}
You can try it at: https://play.golang.org/p/Al7USxzmYST
Changing type fn func(i intf) to type fn func(i int) of course solves the issue. But I don't understand whether it can be made to work with the interface.
You can pass any type of function as an argument. However, when you pass a function as an argument, the function signature has to match. So you cannot pass a function taking an interface in place of a function that takes an int.
Passing an interface value to a function is different than passing an int. When you call a function with an int value, it simply passes that value. But when you pass a int value to a function getting interface{}, it has to create an interface value containing the type of the value and the value itself, and pass that.

Declare function with type alias in Golang

Is it possible to do something like this in Golang?
package main
import "fmt"
type myFunType func(x int) int
var myFun myFunType = myFunType { return x } // (1)
func doSomething(f myFunType) {
fmt.Println(f(10))
}
func main() {
doSomething(myFun)
}
In other words, is it possible to declare a function type variable using a function type alias, without repeating the signature?
Or, alternatively, is there a way not to always retype the whole function-signature, whenever creating a variable of a function-type?
The above code sample, which I would expect to be equivalent to the one below (replace line (1) with line (2)), results in the compilation error syntax error: unexpected return, expecting expression.
package main
import "fmt"
type myFunType func(x int) int
var myFun myFunType = func(x int) int { return 2 * x } // (2)
func doSomething(f myFunType) {
fmt.Println(f(10))
}
func main() {
doSomething(myFun)
}
From Spec: Function literals:
FunctionLit = "func" Signature FunctionBody .
A function literal must contain the func keyword and the Signature. Using a function type is not allowed by the syntax.
Same goes for Function declarations:
FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .
Using a function type (instead of the signature) is not allowed.
So no, what you want is not possible. And the reason for it is because the signature (the function type) does not include the parameter names (just their order and types), but when you are actually "creating" a function value, you need a way to refer to them, and having just the function type you don't have names for the parameters.
See related questions for more details:
Getting method parameter names in Golang
Is unnamed arguments a thing in Go?
No, but in golang you can define methods with a name and use them.
As an example. Sometimes at the top of a file or even in a whole package there is a common way of defining errors like this:
ErrFileNotFound := func(file string) error { return errors.New(fmt.Sprintf("file not found %v", file)) }
And this function can then be used multiple times in the file like
file, err := os.Open(filenameRequestedToOpen) // For read access.
if err != nil {
log.Fatal(ErrFileNotFound(filenameRequestedToOpen))
}
or see https://play.golang.org/p/CvBGGc3YeX4
var myFun myFunType = func(x int) int { return 2 * x } // (2)
this is ok, myFun must contains func keyword,means it's a func type of myFunType, and parameters and return type must same with myFunType declared.

Why struct I pass does not change

I am passing a struct to a function by reference.
I was expecting if I define and change the struct inside the function I can get the new value outside.
But it is not happening.
Can anyone explain why?
package main
import "fmt"
func intbyRef(i *int) {
*i = 10
}
type ttt struct {
a int
}
func change(t *ttt) {
var p ttt = ttt{7}
fmt.Println(p)
t = &p
}
func main() {
i := 1
var t *ttt
fmt.Println(i)
fmt.Println(t)
change(t)
intbyRef(&i)
fmt.Println(i)
fmt.Println(t)
}
You can try the code in here: https://play.golang.org/p/I-GIdIZ9c6
You are not changing the struct inside the function, you are changing the value by setting it to a different memory address. In other words, you're not changing the object stored at the address referenced by t, you're changing the pointer value of t itself, which will not change the pointer value of the t variable outside the function (because Golang is pass by value).
In order to do what you want, the code should look similar to what you're doing for intbyRef, namely:
func change(t *ttt) {
var p ttt = ttt{7}
fmt.Println(p)
*t = p
}
however, this will panic with a nil-pointer dereference. Your main function should also do what you're doing with the int:
func main() {
i := 1
// var t *ttt
t := new(ttt)
...
}
Full code below (playground link here):
package main
import "fmt"
func intbyRef(i *int) {
*i = 10
}
type ttt struct {
a int
}
func change(t *ttt) {
var p ttt = ttt{7}
fmt.Println(p)
// t = &p
*t = p
}
func main() {
i := 1
// var t *ttt
t := new(ttt)
fmt.Println(i)
fmt.Println(t)
change(t)
intbyRef(&i)
fmt.Println(i)
fmt.Println(t)
}
Also, you may want to be guarding against nil values and returning errors, especially for functions internal to your package.
In our code, you are creating new object of ttt in function change and assigning it to t which is passed as parameter to function. In go parameters are passed by value, so when at the end of function change you assign value to t is only for the scope of the function. In order to propagate change to calling function return value from change and assign it back.
Have made the changes to your code, please check play ground link
https://play.golang.org/p/S3GK0JLDHn
You are passing initialized pointer value to intByRef and change the dereferenced value.
In the change you are passing not initialized pointer value (aka nil) and assigning another pointer to it.
So you are doing two different things.
You should know that when you pass a pointer to a function you pass a copy of that pointer (pointing to the same value). That's why main's t is unchanged after passing it to change. It points to the "old" memory address.
If you want to change a value of ttt pointer passed to the function you can do it like you do it in intByRef, but the pointer must be initialized (aka. allocated). Otherwise you'd try to dereference nil.
playground
func change(t *ttt) {
var p ttt = ttt{7}
fmt.Println(p)
*t = p
}
func main() {
t := new(ttt)
fmt.Println(t)
change(t)
fmt.Println(t)
}

Why compiler treats closures and local functions differently?

I thought closures and functions are same thing. But when referencing to a property inside local function compiler doesn't require self. But inside closure it requires to write self. What i mean is why this two things are different?
The sample code for clarity:
class Foo {
let bar = "bar"
func baz() {
func localBaz() {
println(bar) // No complain from compiler.
}
let bazClosure = {
println(self.bar) // Here if I write just println(bar), compiler complains.
}
}
}
You expectation is wrong - functions and closures in Swift are not the same thing. A func essentially sets up a lazy var binding with a [unowned self] declaration. Thus, if you want to get rid of func you could transform the following:
class Foo {
let bar = "bar"
// this is not your 'baz'; just an example
func baz () { println (bar) }
}
}
as
class Foo {
let bar = "bar"
lazy var baz = { [unowned self] in println (self.bar) }
}
You can see that func is doing more than just a closure.
Furthermore, and importantly, func sets up a recursive binding environment which allows the body of func bar to reference bar. Thus you can write:
1> class Foo {
2. func fact (x:Int) -> Int {
3. if 1 == x { return x }
4. else { return x * fact (x - 1) }}
5. }
6> Foo().fact(5)
$R0: (Int) = 120
but not
7> class Foo {
8. lazy var fact = { (x:Int) -> Int in
9. if 1 == x { return x }
10. else { return x * fact (x - 1) }}}
repl.swift:10:27: error: variable used within its own initial value
else { return x * fact (x - 1) }}}
^
Indeed, I do not know why closure need self in swift to access instance properties but let's think about it.
Your baz() is a class function, I mean it belongs to the class Foo and the closure like a external function. In Objective-C all class function actually need a self argument to invoke that function.
Therefore a closure need a self pointer (or something named self reference the instance of Foo) to access its property.

Dynamically create variables of certain type based on string in Go

Simple version
How can you create a variable of a certain type based upon the value of a string?
type ta struct { a int }
type tb struct { b float }
type tc struct { c string }
t := "tb"
v := MagicVarFunc(t) // Returns a new allocated var of type interface{}
v.(tb).b = 8.3
The true example
In my, surprisingly enough, working example below, I am dynamically creating variables based on a string. This is done by registering each struct type in a map with the string being the key and a nil-pointer of the type being the value.
Each type implements an interface with the method New() which returns a new variable of that specific type.
The example below is very close to what I wish to do, where each action has a set of JSON encoded data which will populate the corresponding struct. The way I've structured it is also because I wish to be able to create new stand alone actions that I register to the map.
I am not sure if am abusing the language now.
May anyone give me any pointers if I am completely out of my mind? Is there an obviously easier way?
package main
import (
"fmt"
"encoding/json"
)
// All I require of an action is that it may be executed
type ActionHandler interface {
Exec()
New() ActionHandler
}
// My list of actions
var mActions = make(map[string]ActionHandler)
// Action Exit (leaving the program)
type aExit struct {}
func (s *aExit) Exec() { fmt.Println("Good bye") }
func (s *aExit) New() ActionHandler { return new(aExit) }
func init() {
var a *aExit
mActions[`exit`] = a
}
// Action Say (say a message to someone)
type aSay struct {
To string
Msg string
}
func (s *aSay) Exec() { fmt.Println(`You say, "` + s.Msg + `" to ` + s.To) }
func (s *aSay) New() ActionHandler { return new(aSay) }
func init() {
var a *aSay
mActions[`say`] = a
}
func inHandler(action string, data []byte) {
a := mActions[action].New()
json.Unmarshal(data, &a)
a.Exec()
}
func main(){
inHandler(`say`, []byte(`{"to":"Sonia","msg":"Please help me!"}`))
inHandler(`exit`, []byte(`{}`))
}
You can use reflection to get the zero value of, or to allocate a new value (like new) of a type using reflection, if you can get the Type value at runtime. However, I don't think there is a way to get the Type from a string. You would need to have a value of that type to get the type itself.
I adopted your idea, of using a map. I map the string to the type itself, which you can get using reflect.TypeOf, which gets the type out of an interface value. Then I used reflect.Zero to get the zero value of that type (a convenient value that exists for every type). Then I got the value out as an interface.
package main
import "reflect"
type ta struct { a int }
type tb struct { b float64 }
type tc struct { c string }
var mActions map[string]reflect.Type = make(map[string]reflect.Type)
func init() {
var a ta
mActions[`ta`] = reflect.TypeOf(a)
var b tb
mActions[`tb`] = reflect.TypeOf(b)
var c ta
mActions[`tc`] = reflect.TypeOf(c)
}
func MagicVarFunc(action string) interface{} {
return reflect.Zero(mActions[action]).Interface()
}
func main() {
t := "tb"
v := MagicVarFunc(t) // Returns a new allocated var of type interface{}
x := v.(tb)
x.b = 8.3
}
jorelli's answer is very good. I'm just going to show a few options. Your "true example" looks essentially like command dispatch, with command parameters specified with JSON. To start with simple code that does this,
package main
import (
"encoding/json"
"fmt"
)
func inHandler(action string, data []byte) {
arg := make(map[string]interface{})
json.Unmarshal(data, &arg)
switch action {
case "say":
fmt.Printf("You say, %q to %s\n", arg["msg"], arg["to"])
case "exit":
fmt.Println("Good bye")
}
}
func main() {
inHandler(`say`, []byte(`{"to":"Sonia","msg":"Please help me!"}`))
inHandler(`exit`, []byte(`{}`))
}
Your register new commands by adding cases to the switch statement. Yeah, didn't think you'd like that. So, adding your map and init() idea,
package main
import (
"encoding/json"
"fmt"
)
type jmap map[string]interface{}
var mActions = map[string]func(jmap){}
func init() {
mActions["say"] = func(arg jmap) {
fmt.Printf("You say, %q to %s\n", arg["msg"], arg["to"])
}
}
func init() {
mActions["exit"] = func(jmap) { fmt.Println("Good bye") }
}
func inHandler(action string, data []byte) {
args := make(jmap)
json.Unmarshal(data, &args)
mActions[action](args)
}
func main() {
inHandler(`say`, []byte(`{"to":"Sonia","msg":"Please help me!"}`))
inHandler(`exit`, []byte(`{}`))
}
Now if you wanted, you could put each of those init functions in a separate source file and new commands could be registered by creating a new source file with a new init function.
The rest of the program is simplified with some assumptions that the commands have flat argument lists that the JSON will always encode as an object. This allows you to dispense with separate Go struct definitions for each command. inHandler just creates the same type of object (a map) for all commands, unmarshals into it, and passes it to the command. If you wanted to handle a little more arbitrary JSON, you could unmarshal into an empty interface, and the functions would have to do some extra work to dig out the arguments. If that was too much work and you really wanted to unmarshal directly into a struct, then you arrive near jorelli's solution of making each command function unmarshal its own JSON.
start by defining a function type that does the thing you want:
type Producer func([]byte) interface{}
make a few of them:
func FooProducer(raw []byte) interface{} {
foo := new(Foo)
... // do something to foo
return foo
}
func BarProducter(raw []byte) interface{} {
bar := new(Bar)
... // do something to bar
return bar
}
stick them in a map:
likeThis := map[string]Producer{
"foo": FooProducer,
"bar": BarProducer,
}
and then just do one of these:
myVal := likeThis[someString](raw)
but you probably want to define some interface and make your producer something more like:
type Producer func([]byte) MyAwesomeInterface
since there's probably some common stuff you want to do with those things you're decoding. You also probably want to handle the case of a bad string input, like-a-this:
f, ok := likeThis[someString]
if !ok {
// return, break, panic... something, just get the hell away from here.
}
myVal := f(raw)
The whole notion of inspecting types is kinda cumbersome in Go. It's generally less work to just add new types than it is to try to do reflection gymnastics with the type system.