Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to test a URL via a Go function. In our environment, we have our hostnames set up per environment, like https://www/examplesite20193.domain.org Here is a simple example of what I've got:
package main
import (
"fmt"
"net/http"
"log"
)
func main() {
versionMaj := "2019"
versionMin := "3"
endpoint := versionMaj+versionMin
resp, err:= http.Get("https://www.examplesite%s.domain.org", endpoint)
if err != nil {
log.Fatal(err)
}
fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
}
When I test this, it's stating too many arguments in call to http.Get have (string, string) want (string).
Is there a way to pass in a parameter, like the endpoint one I have specified?
Probably you're looking to something like this:
resp, err:= http.Get(fmt.Sprintf("https://www.examplesite%s.domain.org", endpoint))
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I'm trying to create a Value function for a jsonb database field, for use by go-pg.
func (m JSONMap) Value() (driver.Value, error) {
raw, err := json.Marshal(m)
if err != nil {
return driver.Value(nil), err
}
return driver.Value(raw), nil
}
This seems to be a pretty standard piece of code. Many more or less identical pieces of code can be found here: https://golang.hotexamples.com/examples/database.sql.driver/-/Value/golang-value-function-examples.html and very similar code can be found in the go-pg tests: https://github.com/go-pg/pg/blob/782c9d35ba243106ba6445fc753c3ac6a14c3324/conv_test.go
But no matter what I do, I get a compiler error: cannot convert raw (variable of type []byte) to driver.Value
If I replace return driver.Value(raw), nil with return driver.Value("ABC"), nil it seems to work just fine. However,
str := "ABC"
return driver.Value(str), nil
generates a compiler error.
I'm a little lost, any help is appreciated.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I have the following golang code which trying to access elements on array my expectation to print bxar ,but it throw error any idea?
package main
import (
"encoding/json"
"fmt"
)
type Data struct {
Args struct {
Foo string
}
}
func main() {
in := `[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]}`
var d []Data
json.Unmarshal([]byte(in), &d)
fmt.Println("Foo:", d[1].Args.Foo)
//fmt.Printf("Result: %+v", d)
}
The reason it does not work is a typo. There is one too many } in your JSON:
Before:
`[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]}`
After:
`[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]`
See this playground: https://go.dev/play/p/sL8Cx8lF6WR
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am unable to parse json that has string keys and array as value ending up with json: Unmarshal(non-pointer map[string]interface {}) error.
package main
import (
"encoding/json"
"fmt"
)
func main() {
var s map[string]interface{}
err := json.Unmarshal([]byte("{\"a\":[1,2,3]}"), s)
if err != nil {
panic(err)
}
fmt.Println("Nice parse!")
}
https://go.dev/play/p/AXlF8I-f9-p
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError. Add &s as a parameter
err := json.Unmarshal([]byte("{\"a\":[1,2,3]}"), &s)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to check if time.Now is after another time.Time in Go.
person.CreatedAt is time.Time
if time.Now > person.CreatedAt {
fmt.Println("time.Now is after person.CreatedAt")
}
Here simple example how you can check it:
package main
import (
"fmt"
"time"
)
func main() {
dateFormat := "2006-01-02"
personCreatedAt, err := time.Parse(dateFormat, "2020-01-01")
if err != nil {
// error handling...
}
ok := time.Now().After(personCreatedAt)
fmt.Println(ok)
}
Result will be: true
You can use time.After, time.Before and time.Equal to compare times:
if time.Now().After(person.CreatedAt) {
fmt.Println("time.Now is after person.CreatedAt")
}
To check if a time.Time variable is empty use time.IsZero
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
coders. I'm completely newbie to Go and got a little bit confused about json.Unmarshal output:
package main
import (
"encoding/json"
"fmt"
)
func main() {
s := `[{"First":"James","Last":"Bond","Age":32,"Sayings":["Shaken, not stirred","Youth is no guarantee of innovation","In his majesty's royal service"]},{"First":"Miss","Last":"Moneypenny","Age":27,"Sayings":["James, it is soo good to see you","Would you like me to take care of that for you, James?","I would really prefer to be a secret agent myself."]},{"First":"M","Last":"Hmmmm","Age":54,"Sayings":["Oh, James. You didn't.","Dear God, what has James done now?","Can someone please tell me where James Bond is?"]}]`
var res []struct{}
err := json.Unmarshal([]byte(s), &res)
if err != nil {
fmt.Println(err)
}
fmt.Println(res)
}
Output:
[{} {} {}]
Why is it empty?
You can try it here: https://play.golang.org/p/yztOLJADIXx
If you want to unmarshal JSON objects without knowing their fields, use a map[string]interface{}:
package main
import (
"encoding/json"
"fmt"
)
func main() {
s := `[{"First":"James","Last":"Bond","Age":32,"Sayings":["Shaken, not stirred","Youth is no guarantee of innovation","In his majesty's royal service"]},{"First":"Miss","Last":"Moneypenny","Age":27,"Sayings":["James, it is soo good to see you","Would you like me to take care of that for you, James?","I would really prefer to be a secret agent myself."]},{"First":"M","Last":"Hmmmm","Age":54,"Sayings":["Oh, James. You didn't.","Dear God, what has James done now?","Can someone please tell me where James Bond is?"]}]`
var res []map[string]interface{}
err := json.Unmarshal([]byte(s), &res)
if err != nil {
fmt.Println(err)
}
fmt.Println(res)
}
Try it here: https://play.golang.org/p/iPlBgguE8Kk
However, if you know the names of the fields you're going to unmarshal, you should define the structure. In your case it would look like that:
type Person struct {
First string `json:"First"`
Last string `json:"Last"`
Age int `json:"Age"`
Sayings []string `json:"Sayings"`
}
Try this solution here: https://play.golang.org/p/jCrCteYTaIf