[Solved] Build arrays out of struct

Check it: func main() { type Something struct { Some string Thing int } var props []string var vals []string m := Something{“smth”, 123} s := reflect.ValueOf(m) for i := 0; i < s.NumField(); i++ { props = append(props, s.Type().Field(i).Name) vals = append(vals, fmt.Sprintf(“%v”, s.Field(i).Interface())) } fmt.Println(props) fmt.Println(vals) } Result: [Some Thing] [smth 123] It … Read more

[Solved] Why does my crypt package give me invalid magic prefix error?

Why does this happen and how do I resolve it? You have an invalid magic prefix. github.com/tredoe/osutil/user/crypt/sha512_crypt/sha512_crypt.go if !bytes.HasPrefix(salt, c.Salt.MagicPrefix) { return “”, common.ErrSaltPrefix } Read the crypt package code. PHP: crypt — One-way string hashing PHP: password_hash — Creates a password hash Read the PHP documentation. See your earlier question: golang equivalent of PHP … Read more

[Solved] how to send dynamic json key/value as request params

You can greatly simplify your code by using a proper struct for your JSON unmarshaling: type Request struct { URL string `json:”url”` Params map[string]interface{} `json:”params”` Type string `json:”type”` } Then you can unmarshal it more simply as so: request := &Request{} if err := json.Unmarshal([]byte(j), &request); err != nil { panic(err) } And access the … Read more

[Solved] How to unmarshal struct into map in golang [closed]

Here is an example with marshal and unmarshal using your object definition package main import ( “encoding/json” “fmt” ) type MyObject struct { ID string `json:”id”` Name string `json:”name”` Planets map[string]int `json:”planets”` } func main() { aa := &MyObject{ ID: “123”, Name: “pepe”, Planets: map[string]int{ “EARTH”: 3, “MARS”: 4, }, } // Marshal out, err … Read more

[Solved] Go lang, don’t understand what this code does

tick is a channel in Go. If you look at the docs, tick should send something to the channel once each time interval, which is specified by time.Duration(1000/config.Samplerate) * time.Millisecond in your code. <-tick just waits for that time interval to pass. i keeps track of how many seconds pass, so every time it ticks, … Read more

[Solved] What are Unicode codepoint types for?

Texts have many different meaning and usages, so the question is difficult to answer. First: about codepoint. We uses the term codepoint because it is easy, it implies a number (code), and not really confuseable with other terms. Unicode tell us that it doesn’t use the term codepoint and character in a consistent way, but … Read more

[Solved] Golang libphonenumber [closed]

The answer which I was looking is how to get the country code by passing the phone number only, this is the solution which is working perfectly. num, err := phonenumbers.Parse(“+123456789”, “”) if err != nil { fmt.Println(err.Error()) } regionNumber := phonenumbers.GetRegionCodeForNumber(num) countryCode := phonenumbers.GetCountryCodeForRegion(regionNumber) fmt.Println(countryCode) Thank you Yacacov for the hint 😉 solved Golang … Read more

[Solved] Daisy chain input,output channels together in golang

You have to declare your channel variable outside the loop if you want to re-use it in each iteration (errors and context omitted for brevity): package main import “fmt” func main() { var pipeline Pipeline pipeline.Steps = append(pipeline.Steps, AddBang{}, AddBang{}, AddBang{}, ) src := make(chan Message) pipe := src for _, s := range pipeline.Steps … Read more

[Solved] Golang import struct and share over all app

you might want to solve this like: // main.go import “testapp/app” func main(){ a := app.GetApp() err := a.ConnectDatabase() if err != nil { panic(err.Error()) } a.db. //interesting db code here } // testapp/app.go func (a *App) ConnectDatabase() error{ db, err := sql.Open() if err != nil { return err } a.db = db return … Read more

[Solved] Convert map values to plain strings without brackets? [closed]

From the below information provided by you: when I iterate over it and return the values they return [hello] [world] It seems that your currentMap actually stores string slices []string as values, behind the interface{} type. Assuming that above line means that you see this when printing the map using fmt.Println(), or similar functions. map[first:[hello] … Read more

[Solved] i don’t understand the code : result = quote123(func(x int) string { return fmt.Sprintf(“%b”, x) })

quote123 accepts any function that takes an integer argument and returns a string. The argument passed to it in this code is a function literal, also known as an enclosure or an anonymous function, with this signature. The function literal has two parts: func(x int) string This is the signature of the functional literal. This … Read more