I am trying to figure out how to set up a basic endpoint in golang. I have a call that takes a payload and adds it to a list (in memory right now). Previously it was just returning the exact payload it was getting. I would like it to return a new json object with a randomly generated string. Here it what it looked like initially :
in my repo.go
func RepoCreateActivity(t Activity) Activity {
activities = append(activities, t)
return t
}
and this works fine. However I would like to return a different object with randomly generated string, here is what I have tried
in my repo.go (the randomstring function is from a bit of googling - https://siongui.github.io/2015/04/13/go-generate-random-string/ )
func RandomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func RepoCreateActivity(t Activity) ActivityReturn {
activities = append(activities, t)
b := ActivityReturn({Cgid: RandomString(10)})
return b
}
The actvitityReturn.go looks like :
package main
type ActivityReturn struct {
Cgid string `json:"cgid"`
}
I cannot seem to get this right. I just want the call to return a
{ cgid : "random string here" }
. I am still working through learning golang and any advice would be greatly appreciated. Thanks!