Nowhere in the piece of code you pasted you are calling a function, you are trying to call a struct method on the other hand ( method.Call ). Your error comes from something else and not the fact Go removes unused functions.
Furthermore, if the number of possible struct methods to call is finite, you don't need reflection, you can use a switch statement, thus keeping your code type safe at compilation time. Le mercredi 21 juin 2017 16:18:20 UTC+2, John Kenedy a écrit : > > Below is some part of the code > > > func (*ScrapperMin) ExecuteFunction(funcname string, obj interface{}, args > [][]string) (interface{}, error) { > inputs := make([]reflect.Value, 0) > inputs2 := make([]string, 0) > //var single = true > var tt = make([]reflect.Value, 0) > for i, _ := range args { > if len(args[i]) == 1 { > tt = append(tt, reflect.ValueOf(args[i][0])) > inputs2 = append(inputs2, args[i][0]) > } else { > tt = append(tt, reflect.ValueOf(args[i])) > inputs2 = append(inputs2, args[i][0]) > //single = false > } > inputs = append(inputs, reflect.ValueOf(tt)) > } > method := reflect.ValueOf(obj).MethodByName(funcname) > if method.IsValid() == false { > return nil, errors.New("Invalid Method Name") > } > > ret := method.Call(tt) > return ret, nil > } > > obj is a struct object that has 3 possibilities, a SyntaxCommand struct, a > MathInteger struct or a CookieWebClient struct which contains different set > of functions. > The funcname is the function name intended to call on this object which is > retrieved from file during runtime. > > Questions : > 1. [][]args, if the second dimension slice has only one slice string (len > 1), it will assumg the function accept string as argument, if it is more > than 1 string it will pass an array, I am not sure > reflect.ValueOf([]string) will be correct to pass array of string to > destination function.. The second argument is second parameter of the > function. > 2. The program keep crashing and said invalid address, so I am assuming > the function didn't get compiled into binaries because it was never refer > in any part of the code > 3. the method.IsValid() returns true, it seem to be able to know it is a > correct method, but I am not sure the mechanism > > Thank you for your help > > On Wednesday, June 21, 2017 at 8:44:55 PM UTC+8, John Kenedy wrote: >> >> I read somewhere that Go will remove unused functions so that it wont be >> included into binaries when it can prove no code is using it. >> >> However I am using reflection to call functions that are inside the >> binaries without first having any variable using it. >> >> Is there compiler option to include every functions in source code into >> binary so that it will be used at runtime upon request? >> >> PS : Please don't suggest to call those unused functions and discard the >> output just to make it compiles into binary >> >> Thank you >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.