How to get function arguments in Go at runtime, all I know is only how to get function name:
pc, file, line, ok := runtime.Caller(2)
rt := runtime.FuncForPC(pc)
return rt.Name() // Foo
What I need is something like this:
Foo(1,2,3)
// Foo_1_2_3
Not a full answer, but maybe this can help :
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println(reflect.TypeOf(f1))
for index := 0; index < reflect.TypeOf(f1).NumIn(); index++ {
fmt.Println(reflect.TypeOf(f1).In(index))
}
}
func f1(a int, b string) {}
prints :
func(int, string)
int
string
Related
Give a function:
type myFunc func(...interface{}) (interface{})
I'd like to get the type of myFunc, something like:
t := reflect.TypeOf(myFunc)
Or
t := reflect.TypeOf((*myFunc)(nil))
The only way I have found to do this is by first creating a temporary variable of myFunc and then getting the TypeOf from that.
var v myFunc
t := reflect.TypeOf(v)
Is there a better way to do this?
The simplest way to get the function type would be reflect.TypeOf(myFunc(nil)).
package main
import (
"fmt"
"reflect"
)
type myFunc func(...interface{}) interface{}
func main() {
t := reflect.TypeOf(myFunc(nil))
// Print whether function type is variadic
fmt.Println("Variadic =", t.IsVariadic())
// Print out every input type
for i := 0; i < t.NumIn(); i++ {
fmt.Println("In", i, "=", t.In(i))
}
// Print out every output type
for i := 0; i < t.NumOut(); i++ {
fmt.Println("Out", i, "=", t.Out(i))
}
}
package main
import (
"fmt"
)
type alias int
type aliases []*alias
func main() {
a1 := alias(1)
t := aliases{&a1}
fmt.Println([]*int([]*alias(t)))
}
The type type aliases []*alias is essentially []*int
I want to be able to type convert aliases back to []*int
You can with unsafe.Pointer, a little bit unsafe so not recommended
PointerToSliceOfPointersToInt := (*([]*int))(unsafe.Pointer(&t))
try it works https://play.golang.org/p/6AWd1W_it3
Try this, you could do that by doing right casting.
type alias int
type aliases []*alias
func main() {
a1 := alias(1)
t := aliases{&a1}
orig := int(*([]*alias(t)[0]))
fmt.Println(orig)
}
Example on http://play.golang.org/p/1WosCIUZSa
If you want to get all values (not just the first index) you have to loop and cast each element.
func main() {
a1 := alias(1)
t := aliases{&a1}
orig := []*int{}
for _, each := range t {
temp := int(*each)
orig = append(orig, &temp)
}
fmt.Printf("%#v\n", orig) // []*int{(*int)(0x10434114)}
}
Example: http://play.golang.org/p/Sx4JK3kA45
I found this question with this great answers:
How to find a type of a object in Golang?
I played around with the answer and tried to get the name of a struct in the same way:
package main
import (
"fmt"
"reflect"
)
type Ab struct {
}
func getType(myvar interface{}) string {
return reflect.TypeOf(myvar).Name()
}
func main() {
fmt.Println("Hello, playground")
tst := "string"
tst2 := 10
tst3 := 1.2
tst4 := new(Ab)
fmt.Println(getType(tst))
fmt.Println(getType(tst2))
fmt.Println(getType(tst3))
fmt.Println(getType(tst4))
}
Go playground: http://play.golang.org/p/tD8mygvETH
But the output is:
Hello, playground
string
int
float64
Program exited.
Expected output would be:
Hello, playground
string
int
float64
Ab
Program exited.
I tried to figure out by reading the documentation but didn't find the issue about that. So, sorry for the very general question, but:
What's the reason, reflect.TypeOf().Name() does not work with (this) struct(s)?
In your example you pass a value of pointer type (*Ab), not a struct type.
Sticking to Type.Name()
If it is not a pointer, Type.Name() will properly return Ab. In case of pointer if you still want the struct's name, you can use Type.Elem() to get the element's type:
func getType(myvar interface{}) string {
if t := reflect.TypeOf(myvar); t.Kind() == reflect.Ptr {
return "*" + t.Elem().Name()
} else {
return t.Name()
}
}
Testing it:
tst4 := Ab{}
tst5 := new(Ab)
fmt.Println(getType(tst4))
fmt.Println(getType(tst5))
Output (try your modified example on the Go Playground):
Ab
*Ab
Note:
Note that as Type.Name() does not resolve pointers, it would not work if the value passed is a pointer to pointer, e.g. **Ab, while as Type.String() automatically resolves pointers, would work in this case too.
We can easily make our getType() function to work with **Ab too (or with any depth of pointers):
func getType(myvar interface{}) (res string) {
t := reflect.TypeOf(myvar)
for t.Kind() == reflect.Ptr {
t = t.Elem()
res += "*"
}
return res + t.Name()
}
Calling it with values:
tst4 := Ab{}
tst5 := new(Ab)
tst6 := &tst5 // type of **Ab
tst7 := &tst6 // type of ***Ab
Output (try it on the Go Playground):
Ab
*Ab
**Ab
***Ab
Using Type.String()
A simpler and better approach would be to use Type.String() instead of Type.Name() which automatically handles pointers and also includes package name. E.g.:
func getType(myvar interface{}) string {
return reflect.TypeOf(myvar).String()
}
For the modified example it outputs:
string
int
float64
main.Ab
*main.Ab
Try this variant on the Go Playground.
fmt has a cool %T tag as well
package main
import (
"fmt"
"net/http"
)
type Potato struct {
}
func main() {
fmt.Printf("I have a %T, an %T and a %T\n", Potato{}, http.StatusMultipleChoices, &http.Response{})
}
outputs I have a main.Potato, an int and a *http.Response
https://play.golang.org/p/6z7_0BSitm
The problem is new returns pointer, following should get the desired result.
package main
import (
"fmt"
"reflect"
)
type Ab struct {
}
func getType(myvar interface{}) {
valueOf := reflect.ValueOf(myvar)
if valueOf.Type().Kind() == reflect.Ptr {
fmt.Println(reflect.Indirect(valueOf).Type().Name())
} else {
fmt.Println(valueOf.Type().Name())
}
}
func main() {
fmt.Println("Hello, playground")
tst := "string"
tst2 := 10
tst3 := 1.2
tst4 := new(Ab)
getType(tst)
getType(tst2)
getType(tst3)
getType(tst4)
}
Output is
Hello, playground
string
int
float64
Ab
How can I access a capture group from inside ReplaceAllFunc()?
package main
import (
"fmt"
"regexp"
)
func main() {
body := []byte("Visit this page: [PageName]")
search := regexp.MustCompile("\\[([a-zA-Z]+)\\]")
body = search.ReplaceAllFunc(body, func(s []byte) []byte {
// How can I access the capture group here?
})
fmt.Println(string(body))
}
The goal is to replace [PageName] with PageName.
This is the last task under the "Other tasks" section at the bottom of the Writing Web Applications Go tutorial.
I agree that having access to capture group while inside of your function would be ideal, I don't think it's possible with regexp.ReplaceAllFunc.
Only thing that comes to my mind right now regard how to do this with that function is this:
package main
import (
"fmt"
"regexp"
)
func main() {
body := []byte("Visit this page: [PageName] [OtherPageName]")
search := regexp.MustCompile("\\[[a-zA-Z]+\\]")
body = search.ReplaceAllFunc(body, func(s []byte) []byte {
m := string(s[1 : len(s)-1])
return []byte("" + m + "")
})
fmt.Println(string(body))
}
EDIT
There is one other way I know how to do what you want. First thing you need to know is that you can specify non capturing group using syntax (?:re) where re is your regular expression. This is not essential, but will reduce number of not interesting matches.
Next thing to know is regexp.FindAllSubmatcheIndex. It will return slice of slices, where each internal slice represents ranges of all submatches for given matching of regexp.
Having this two things, you can construct somewhat generic solution:
package main
import (
"fmt"
"regexp"
)
func ReplaceAllSubmatchFunc(re *regexp.Regexp, b []byte, f func(s []byte) []byte) []byte {
idxs := re.FindAllSubmatchIndex(b, -1)
if len(idxs) == 0 {
return b
}
l := len(idxs)
ret := append([]byte{}, b[:idxs[0][0]]...)
for i, pair := range idxs {
// replace internal submatch with result of user supplied function
ret = append(ret, f(b[pair[2]:pair[3]])...)
if i+1 < l {
ret = append(ret, b[pair[1]:idxs[i+1][0]]...)
}
}
ret = append(ret, b[idxs[len(idxs)-1][1]:]...)
return ret
}
func main() {
body := []byte("Visit this page: [PageName] [OtherPageName][XYZ] [XY]")
search := regexp.MustCompile("(?:\\[)([a-zA-Z]+)(?:\\])")
body = ReplaceAllSubmatchFunc(search, body, func(s []byte) []byte {
m := string(s)
return []byte("" + m + "")
})
fmt.Println(string(body))
}
If you want to get group in ReplaceAllFunc, you can use ReplaceAllString to get the subgroup.
package main
import (
"fmt"
"regexp"
)
func main() {
body := []byte("Visit this page: [PageName]")
search := regexp.MustCompile("\\[([a-zA-Z]+)\\]")
body = search.ReplaceAllFunc(body, func(s []byte) []byte {
// How can I access the capture group here?
group := search.ReplaceAllString(string(s), `$1`)
fmt.Println(group)
// handle group as you wish
newGroup := "<a href='/view/" + group + "'>" + group + "</a>"
return []byte(newGroup)
})
fmt.Println(string(body))
}
And when there are many groups, you are able to get each group by this way, then handle each group and return desirable value.
You have to call ReplaceAllFunc first and within the function call FindStringSubmatch on the same regex again. Like:
func (p parser) substituteEnvVars(data []byte) ([]byte, error) {
var err error
substituted := p.envVarPattern.ReplaceAllFunc(data, func(matched []byte) []byte {
varName := p.envVarPattern.FindStringSubmatch(string(matched))[1]
value := os.Getenv(varName)
if len(value) == 0 {
log.Printf("Fatal error substituting environment variable %s\n", varName)
}
return []byte(value)
});
return substituted, err
}
I have my script "file.go" Built with "go build file.go" now I have "file.exe"
In the code I have "steamid := xxxxxxxxxxxxxxxx" Is there anyway when executing file.exe in cmd like "file.exe -steamid=xxxxxxxxxxxxxxxx"
code:
package main
import (
"crypto/md5"
"fmt"
)
func main() {
steamid := xxxxxxxxxxxxxxxx
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(steamid & 0xFF)})
steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
}
I've gotten as far as here with new replys;
package main
import (
"crypto/md5"
"fmt"
"bufio"
"os"
"flag"
)
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "XXXXXXXXXXXXXXXXX", "17 Numbers SteamID")
}
func main() {
steamid := &SteamID
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(steamid & 0xFF)})
steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
fmt.Print("\nPress 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
Error:
C:\Go\bin>go build file.go
# command-line-arguments
.\file.go:24: invalid operation: steamid & 255 (mismatched types *string and int)
.\file.go:25: invalid operation: steamid >>= 8 (shift of type *string)
the flag package included in the standard library does just that.
what you need to add in your script:
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "<insert default value>", "<insert help text>")
}
(in case you need to get it as an integer, use Int64Var instead)
then in your main function add:
flag.Parse()
This will initialise the value of SteamID
It's all in the error message. You can't do bitwise operations with strings, pointers to strings or anything that is not an integer, you need to convert or parse them into integers first. Use strconv.ParseInt and its friends from the strconv package to parse strings.
parsedID, e := strconv.ParseInt(*steamID, 16, 64)
if e != nil { log.Fatal("couldn't parse the ID") }
// Use parsedID.