How do i get the method argument names - go

I have this nice function that prints some info about a given method in a given type, here is a goplay
it does all i need except i can't find a way to get the method argument names,any idea to achieve that?
func AnalyzeMethod(instance interface {},methodName string){
m,ok := reflect.ValueOf(instance).Type().MethodByName(methodName)
if ok{
isVariadic := m.Type.IsVariadic()
var VariadicIndex int
var VariadicType reflect.Type
var FirstPositionalIndex int
var LastPositionalIndex int
var ArgsLength int = m.Type.NumIn()-1
//var []string ArgumentNames //can't find a way to get an ordered slice of names of every argument that the function takes
if ArgsLength>0{
FirstPositionalIndex = 1
}
if isVariadic{
VariadicIndex = ArgsLength
VariadicType = m.Type.In(VariadicIndex)
LastPositionalIndex = ArgsLength-1
} else {
VariadicIndex = -1
LastPositionalIndex = ArgsLength
}
fmt.Println("ArgsLength:",ArgsLength)
fmt.Println("FirstPositionalIndex:",FirstPositionalIndex)
fmt.Println("LastPositionalIndex:",LastPositionalIndex)
fmt.Println("Variadic:",isVariadic)
fmt.Println("VariadicIndex:",VariadicIndex)
fmt.Println("VariadicType:",VariadicType)
fmt.Println("ArgumentNames","How?")
}
}

The names of local variables (including function arguments) are just for the programmer; the compiler discards them when it generates code. There is no way to retrieve argument names as they are not available in the compiled code.

Related

Is it possible to assign to a regular variable and slice in the same statement?

I'm making a chess game and I want to do a series of type assertions in the same var statement, then pass them to a function that handles it, but apparently, Go doesn't allow me to assign to a regular variable and a slice index in the same statement:
// inside a function:
asserts := make([]bool, 0, 10)
assertionHandler := func(ok *[]bool) {
for _, b := range *ok {
if !b {
msg := "pieceCliked: failed while trying to do type assertion\n%s\n\n"
utils.LogPrintError(errors.New(fmt.Sprintf(msg, string(debug.Stack()))))
}
}
*ok = make([]bool, 0, 10)
}
var (
possibleSquares []string
// The following results in a syntax error: expected type, found '='
dataObject, asserts[0] = data.(map[string]any)
playerData, asserts[1] = dataObject["playerData"].(map[string]any)
square, asserts[2] = playerData["selectedPieceLocation"].(string)
piece, asserts[3] = playerData["selectedPiece"].(string)
color, asserts[4] = playerData["selectedPieceColor"].(string)
)
assertionHandler(asserts)
Is it possible to do what I'm trying to do?
Not the way you're doing it, no. A var block defines new variables and their types, but you're trying to assign to both new variables with no types (hence the error expected type) and elements of an existing slice within that block.
You could do:
var (
possibleSquares []string
dataObject map[string]any
playerData map[string]any
square string
piece string
color string
)
dataObject, asserts[0] = data.(map[string]any)
playerData, asserts[1] = dataObject["playerData"].(map[string]any)
square, asserts[2] = playerData["selectedPieceLocation"].(string)
piece, asserts[3] = playerData["selectedPiece"].(string)
color, asserts[4] = playerData["selectedPieceColor"].(string)
Another answer describes why the code in the question does not work. Here's another workaround:
Write assertion handler to use variadic argument:
func assertionHandler(asserts ...bool) bool {
result := true
for _, b := range assserts {
if !b {
result = false
msg := "pieceCliked: failed while trying to do type assertion\n%s\n\n"
utils.LogPrintError(errors.New(fmt.Sprintf(msg, string(debug.Stack()))))
}
}
return result
}
Use short variable declarations to collect the values and bool results:
dataObject, assert0 := data.(map[string]any)
playerData, assert1 := dataObject["playerData"].(map[string]any)
square, assert2 := playerData["selectedPieceLocation"].(string)
piece, assert3 := playerData["selectedPiece"].(string)
color, assert4 := playerData["selectedPieceColor"].(string)
if !assertionHandler(assert0, assert1, assert2, assert3, assert4) {
return
}

How to make nil interface to struct in golang

I'm new in Golang.
I executed the code below. I get empty humans array in the end.
What should I do in func F?
For testing(monkeypatch) sake. I have to follow the way how the origin func is called.
package main
import (
"fmt"
)
type Human struct {
Name string
}
type Cat struct {
Name string
}
func F(arr interface{}) {
switch arr.(type) {
case *[]*Human:
arr = &[]*Human{{Name: "abc"}}
arr = arr.(*[]*Human)
case *[]*Cat:
arr = &[]*Cat{{Name: "meow"}}
arr = arr.(*[]*Cat)
}
}
func main() {
var humans []*Human
F(&humans)
fmt.Println(humans)
var cats []*Cat
F(&cats)
fmt.Println(cats)
}
The answer, and the main issue cause as well, is that Go always uses pass by value (or copy of the value) when arguments are passed around to function or assigned to variables.
Your function F takes an arr argument:
func F(arr interface{}) {
//...
}
When called from your main function, you are passing an []*Human pointer as an argument, which values will be copied and fed to your function F for execution.
Going back to your function F body, the arr will be having the same value passed by main, which happens to be the address to the original []*Human struct. Upon assigning a new value to arr:
func F(arr interface{}) {
switch arr.(type) {
case *[]*Human:
arr = &[]*Human{{Name: "abc"}}
// ...
}
}
You are assigning a new value to the local arr variable and not to the original pointer, which remains, indeed, unchanged.
To update the value toward which the argument pointer is referring to, you should used the dereferrence symbol:
func F(arr interface{}) {
switch arr := arr.(type) {
case *[]*Human:
*arr = []*Human{&Human{Name: "abc"}}
fmt.Println(arr)
// ...
}
}
Note the switch arr := arr.(type) statement which creates a new arr variable (shadowing the argument arr) with the interface dynamic type to be able to assign the proper value to it.

Go - The difference between var and func to define a function

I'm coming from an Scala background, and in Scala, you could define functions both as a single value, or an actual function, for instance:
val inc1: Int => Int = _ + 1 // single FUNCTION value
def inc2(x: Int): Int = x + 1 // normal function definition
// in this case "inc1 eq inc1" is true, since this is a single instance
// but "inc2 eq inc2" is false
And these 2 have some differences (i.e., size allocation, first one is a single instance, while the other one returns an instance each time it is invoked, ...), so based on the use case, we could kind of reason which one to use. Now I'm new to golang, and wanted to know if the below 2 function definitions (correct me if I'm wrong with the phrase) differ in Golang, and if so, what are differences?
var inc1 = func(x int) int { return x + 1 }
func inc2(x int) int { return x + 1 }
Thanks in advance!
Scala borrows a lot from functional programming. Go does not.
(If you've used multiple other programming languages, you should definitely read the Go specification. It's not very long as Go is not a very large language, although the new generics definitely complicate things a bit.)
In Go, the func keyword introduces a function definition or function type, with the details being context-dependent. The var keyword introduces a variable declaration.1 So:
func inc2(x int) int { return x + 1 }
defines a function, inc2, whose code is as shown. But:
var inc1 = // ...
declares and then initializes a variable, inc1. The type and initial value of the variable are determined by the commented-out section, so:
var inc1 = func(x int) int { return x + 1 }
defines a function (with no name) whose code is as shown. That function is then assigned to the variable as its initial value, so that the implied type of the variable is func (int) int or function taking one argument of type int and returning one value of type int.
Having created a variable, you can now either call the function currently stored in that variable:
func callit(arg int) {
result := inc1(arg)
// ... do something with the result ...
}
Or you can assign a new value into the variable, e.g.:
func overwrite() {
inc1 = func(a int) int { return a * 2 } // name `inc1` is now misleading
}
Because inc2 is a function, you can't re-assign a new value to it: it's just a function, not a variable.
1Note that a variable declaration with an initialization can use the "short declaration" form:
func f() {
v := 3
// ...
}
where we leave out the type and just say "use the type of the expression to figure out the type of the declaration". This declares and initializes the variable. Short declarations can only appear in block scope, so these must be inside some function. Other than omitting the var keyword they do nothing that you couldn't do by including the var keyword, or sometimes multiple var keywords:
result, err := doit()
might require:
var result someType
var err error
result, err = doit()
when written without using the short-declaration form.

Naming receiving variables from a multiple return function

Go allows for multiple named return values, but what about the receiving variables? Are they protected when return values are juggled around?
Let's say we start with this:
func foo() (i int, j int) {
i = 1
j = 2
return
}
a, b := foo()
Now what if some other coder comes by and makes the following change to foo's definition:
func foo() (j int, i int) {
my calling function is invalidated. Is it, then, possible to name the returned values from the calling side as well. For instance, if I called it like this:
(a:i, b:j) := foo()
then I would be attaching them to the named return values, rather than assigning them in the order they are returned.
So, is there a way to solve that problem?
This is no different than rearranging the input parameters. As a rule, don't do that unless you intend to make a breaking change. But if you want to deal with things by name rather than position, you want a struct. For example, you can use anonymous structs:
func foo() struct {
i int
j int
} {
return struct {
i int
j int
}{1, 2}
}
func main() {
result := foo()
fmt.Println(result.i, result.j)
}
Of course you can also name the struct if you used it in other places, but there's no need if you just want to name the fields.

Is there a shortcut for assigning a variable to a pointer without creating the variable in a separate line first?

If I have a struct like this:
type Message struct {
Id int64
Message string
ReplyTo *int64
}
And then if I did create an instance of this struct like this:
var m Message
m.Id = 1
m.Message = "foo bar yo"
var replyTo = int64(64)
m.ReplyTo = &replyTo
Then it would work.
But I was wondering if there was a shortcut for the last step?
I tried doing something like:
m.ReplyTo = &int64{64}
But it did not work.
I don't think you can because the value is a primitive and attempting to do it in one shot like the below would be a syntax error. Its attempting to get an address of a value so it wouldn't be possible. At least I am not aware of a way where its possible.
someInt := &int64(10) // would not compile
The other alternative you have is to write a function to return a pointer to the primitive like the following:
func NewIntPointer(value int) *int {
return &value
}
A tricky way to get int pointer without create new variable.
someIntPtr := &[]int64{10}[0]

Resources