I have some func saved in a variable and I need to convert it into a string literal:
testFunc := func(orgId int64) bool {
return false
}
The expected result string literal is like this:
resultStr := `func(orgId int64) bool {
return false
}`
Here is what I have tried:
testFuncStr := reflect.ValueOf(testFunc).String()
However this only get me the following literal:
"<func(int64) bool Value>",
As #BurakSerdar said - Go is not a language which is interpreted, but compiled, so there's rather limited amount of stuff you can do. reflect is a powerful package - you can get function names, number of function arguments, and even call stack (which is handy for logging), but you will never be able to obtain a source code of a function from within a build binary.
Related
I'm attempting to implement some Go code to solve a problem in which I need to sufficiently obfuscate a known integer value by converting it into a seemingly random hexadecimal string, when provided a known key value as an additional input parameter. The resulting hexadecimal string needs to always be the same number of characters in length (ideally, <= 32 characters).
Furthermore, using the same key string value, I need to un-obfuscate the hexadecimal string back into the original integer. For additional context, I'd like to satisfy the following function signatures (but am open to alternative methods, if necessary):
func Scramble(key string, value int32) string {
// TODO: Given a known key and value, generate a sufficiently unpredictable hexadecimal string.
}
func Unscramble(key string, value string) int32 {
// TODO: Given a known key and value, generate the integer that created the hexadecimal string.
}
func main() {
key := "Something super secret!"
scrambled := Scramble(key, 135)
fmt.Printf("Scrambled: %s\n", scrambled) // Scrambled: a1dec128b590b9ec3281110d6d188c26
unscrambled := Unscramble(key, scrambled)
fmt.Printf("Unscrambled: %d\n", unscrambled) // Unscrambled: 135
}
I think XOR'ing may be the right direction, but I'm unsure and not particularly familiar with the topic yet.
Any insight/direction would be greatly appreciated! Please let me know if I can provide any additional context/clarifications.
There are many native or external packages to achieve what you want, but if you want to implement this yourself for a learning experience, you can try the following tack:
Rather than shuffle your data back and forth between string and int32 format - keep the data in its raw type and use Stringer methods to convert to hex - and helper methods/functions to convert to the desired type. This simplifies the scrambling/unscrambling logic - as the input types are the same for both.
// Code custom type so we can add stringer methods
type Code uint32
// String converts code to hex string format
func (c Code) String() string {
return fmt.Sprintf("%x", uint32(c))
}
// CodeFromString gets a code from a hex string
func CodeFromString(hexs string) (Code, error) {
ui, err := strconv.ParseUint(hexs, 16, 32)
if err != nil {
return 0, err
}
return Code(ui), nil
}
// XOR scrambles/unscrambles
func XOR(key, value Code) Code {
return key ^ value
}
And to use:
keyHex := "74490a85"
valueHex := "d195c729"
value, _ := CodeFromString(valueHex)
key, _ := CodeFromString(keyHex)
scrambled := XOR(key, value)
unscrambled := XOR(key, scrambled)
Playground Example: https://play.golang.org/p/y5pbac_f8Z1
Absolute newbie question here.
Some functions in Go return more than one value (normally, the value and an error). I was writing a func who return the return value of one of those functions, and even if it is very easy to put the values on variables and return only the first one, I have the doubt if I could do the same in only one line without the extra variable. This is something often uses in other languages like C, Java, C#, Ruby, etc
func someFunc (param string) int {
// do something with the string, not very important
return strconv.Atoi(param)
}
I know this works
func someFunc (param string) int {
// do something with the string, not very important
var result int
result, _ = strconv.Atoi(param)
return result
}
It is this possible in Go? It is considered a "good practice" (like in Java*)
Note: Before someone say that this technique is not a good practice in Java, clarify that is not important for the question, but some people (like the ones in the company I work) encourage that style.
Use a short variable declaration for the shortest code to accomplish this goal:
func SomeFunc(parm string) int {
result, _ := strconv.Atoi(param)
return result
}
There is no one line solution without introducing a helper function that accepts two arguments and returns the first. One of these helper functions would be needed for each combination of types where you want to ignore a value.
Your best possible one-liner is a helper function written as:
func first(n int, _ error) int {
return n
}
func SomeFunc(param string) int {
return first(strconv.Atoi(param))
}
Note that:
the argument types and positions must match exactly
the second argument to first has the blank identifier (_), making it clear that you wish to completely ignore it. [1]
If you absolutely don't want to declare a named function, you may use a function literal, but that looks real ugly:
func SomeFunc(param string) int {
return func(n int, _ error) int { return n }(strconv.Atoi(param))
}
In general, the helper function is worth it if you have a lot of repetition in your code. Otherwise just use a temp variable, which looks clean and idiomatic:
func SomeFunc(param string) int {
n, _ := strconv.Atoi(param)
return n
}
Playground: https://play.golang.org/p/X8EOh_JVDDG
Once generics will be added to the language in Go 1.18, you will be able to write a helper function that can be used with any return pair and preserve type safety on the first one:
func first[T, U any](val T, _ U) T {
return val
}
func SomeFunc(param string) int {
return first(strconv.Atoi(param))
}
Go2 Playground: https://go2goplay.golang.org/p/vLmTuwzrl5o
Footnotes:
[1] Keep in mind that in case of strings.Atoi the second return value is an error, and ignoring errors is bad practice. However there are cases where the success of the operation truly doesn't matter, then it's fine to use _ to ignore the argument.
I am passing a pointer to a string, to a method which takes an interface (I have multiple versions of the method, with different receivers, so I am trying to work with empty interfaces, so that I don't end up with a ton of boilerplate madness. Essentially, I want to populate the string with the first value in the slice. I am able to see the value get populated inside the function, but then for some reason, in my application which calls it, tha value doesn't change. I suspect this is some kind of pointer arithmetic problem, but could really use some help!
I have the following interface :
type HeadInterface interface{
Head(interface{})
}
And then I have the following functions :
func Head(slice HeadInterface, result interface{}){
slice.Head(result)
}
func (slice StringSlice) Head(result interface{}){
result = reflect.ValueOf(slice[0])
fmt.Println(result)
}
and... here is my call to the function from an application which calls the mehtod...
func main(){
test := x.StringSlice{"Phil", "Jessica", "Andrea"}
// empty result string for population within the function
var result string = ""
// Calling the function (it is a call to 'x.Head' because I lazily just called th import 'x')
x.Head(test, &result)
// I would have thought I would have gotten "Phil" here, but instead, it is still empty, despite the Println in the function, calling it "phil.
fmt.Println(result)
}
*NOTE : I am aware that getting the first element doesn't need to be this complicated, and could be slice[0] as a straight assertion, but this is more of an exercise in reusable code, and also in trying to get a grasp of pointers, so please don't point out that solution - I would get much more use out of a solution to my actual problem here * :)
As you said in your NOTE, I'm pretty sure this doesn't have to be this complicated, but to make it work in your context:
package main
import (
"fmt"
"reflect"
)
type HeadInterface interface {
Head(interface{})
}
func Head(slice HeadInterface, result interface{}) {
slice.Head(result)
}
type StringSlice []string
func (slice StringSlice) Head(result interface{}) {
switch result := result.(type) {
case *string:
*result = reflect.ValueOf(slice[0]).String()
fmt.Println("inside Head:", *result)
default:
panic("can't handle this type!")
}
}
func main() {
test := StringSlice{"Phil", "Jessica", "Andrea"}
// empty result string for population within the function
var result string = ""
// Calling the function (it is a call to 'x.Head' because I lazily just called th import 'x')
Head(test, &result)
// I would have thought I would have gotten "Phil" here, but instead, it is still empty, despite the Println in the function, calling it "phil.
fmt.Println("outside:", result)
}
The hard part about working with interface{} is that it's hard to be specific about a type's behavior given that interface{} is the most un-specific type. To modify a variable that you pass as a pointer to a function, you have to use the asterisk (dereference) (for example *result) on the variable in order to change the value it points to, not the pointer itself. But to use the asterisk, you have to know it's actually a pointer (something interface{} doesn't tell you) so that's why I used the type switch to be sure it's a pointer to a string.
I'm trying to develop a routine in Go that will be called by a C++ program. The Go looks like the following:
package main
import (
"C"
"encoding/json"
"log"
)
type keydata struct {
Key string `json:"key"`
Error string `json:"Error"`
}
func lookupKey() string {
//simplified to remove the call to web service
body := "{\"key\": \"blahblah\", \"Error\": \"\"}"
k := keydata{}
err := json.Unmarshal([]byte(body), &k)
if err != nil {
log.Fatal(err)
}
return k.Key
}
//export GetKey
func GetKey() string {
theKey := lookupKey()
return theKey
}
func main() {}
If I substitute some hard coded value for the return k.Key statement everything works fine and the C or C++ can call the exported GetKey function. When I try to return the decoded JSON string from k.Key or even just return the string from the variable named body - I receive an error:
runtime error: cgo result has Go pointer
goroutine 17 [running, locked to thread]
I'm building this as follows:
go build -buildmode=c-archive example.go
The C++ is built as follow:
g++ -pthread test.cpp example.a -o test
What am I missing to make this work without raising a panic error? I'm digging around to find an answer but have yet to resolve this.
#JimB & #Jsor, thank you so much for your responses. Returning a *C.char certainly worked. I'm left wondering though, when I return it as a Go string behind the scenes in the auto generated header file Go actually creates and passes a C struct named GoString that contains a char array named p and the length named n. As long as I pass a hard-coded string instead of k.Key it actually works and I can interrogate the auto-generated char array in C++. When I try to return k.Key, a string it throws that exception. Is it possible to cast the Go string or add some notation to the export decoration to make it work?
I can certainly return the C.CString char array and make it work - thank you! I'm just also wanting to understand why it works when returning a hard coded string and not in the example I've posted.
Thank you both for your time and explanations.
You can't return a Go string to a C function. If you want a C string, you can use the C.CString function to create one and return a *C.char
//export GetKey
func GetKey() *C.char {
theKey := lookupKey()
return C.CString(theKey)
}
The return value from this function must be explicitly freed in the C code.
If freeing the allocated buffer isn't convenient, its common to fill a buffer provided by the caller:
func GetKey(buff *C.char, n int) int
If you can allocate the memory but don't want to handle C strings, you can insert the buffer into a pointer and return the size.
func GetKey(buff **C.char) int
You need to use C.CString to convert Go strings to raw pointers to C strings. Note that C Strings are not garbage collected and must be freed by you elsewhere in the program.
This will make the return type *C.char which should be visible to C as a char array. It will also be your responsibility to return the buffer length (whether your write a separate function or a C struct to do that is up to you).
Is it possible to pass a result form function which returns multiple values directly to function which accepts only one? Example:
func MarshallCommandMap(mapToMarshall map[string]string) string {
return string(json.Marshal(mapToMarshall))
}
The example above will cause compilation error:multiple-value json.Marshal() in single-value context. I know it is possible to get same result with additional variable:
func MarshallCommandMap(mapToMarshall map[string]string) string {
marshaledBytes, marshalingError := json.Marshal(mapToMarshall)
if (marshalingError != nil) {
panic(marshalingError)
}
return string(marshaledBytes)
}
But is it possible to pass only first value direclty without any variable?
I think you mean doing something like python's tuple unpacking.
Unfortunately this is not possible in Go (AFAIK).
No you can't, however 2 things with your code.
Shouldn't panic, either return an error or return an empty string.
You can make it shorter.
Example :
func MarshallCommandMap(mapToMarshall map[string]string) string {
js, _ := json.Marshal(mapToMarshall) //ignore the error
return string(js)
}