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)
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 1 year ago.
Improve this question
Is it a normal behavior when parsing uint64 max value with strconv.ParseInt?
i, err := strconv.ParseInt("18446744073709551615", 10, 64)
fmt.Println(i, err)
I got an error: "strconv.ParseInt: parsing "18446744073709551615": value out of range", when maximum allowed value for uint64 is: 18446744073709551615
Can you explain such behavior?
https://golang.org/src/builtin/builtin.go?s=1026:1044#L26
Call ParseUint to parse an unsigned integer.
The ParseInt function parses signed integers. The maximum signed integer is 9223372036854775807.
Based the comments ,I reproduced your code as follows:
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.ParseUint("18446744073709551615", 10, 64)
fmt.Println(i, err)
}
Output:
18446744073709551615 <nil>
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
In my GoLang program which invokes a REST API, i need to collect the responses from different REST API's which return slices of pointers of the same struct.
I am attempting to concatenate the slices of pointers using append and i am getting error similar to what is shown below.
I think append does not support such an operation , is there any alternative to this ?
cannot use response (type []*string) as type *string in append
A go playground link for the problem ,i am trying to demonstrate is given here.
https://play.golang.org/p/lnzSd2kbht0
package main
import (
"fmt"
)
func main() {
var fruits []*string
response := GetStrings("Apple")
fruits = append(fruits, response...)
response = GetStrings("Banana")
fruits = append(fruits, response...)
response = GetStrings("Orange")
fruits = append(fruits, response...)
if fruits == nil || len(fruits) == 0 {
fmt.Printf("Nil Slice")
} else {
fmt.Printf("Non nil")
fmt.Printf("%v", fruits)
}
}
func GetStrings(input string) []*string {
var myslice []*string
myslice = append(myslice, &input)
return myslice
}
I cannot change the REST API or the function signature to return the slice of structs itself.
To append all elements of a slice to another slice, use:
resultSlice=append(slice1, slice2...)
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
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))