Golang access nested element on Json [closed] - go

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

Related

How to parse map[string]interface{} [closed]

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)

How to check if one time.Now is after another time.Time [closed]

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

Testing a URL with a parameter in Golang [closed]

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))

Go newbie: Getting "mismatched types *int and int" error when trying to compare reference to static integer [closed]

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 learning Go and trying to use reference to integer value in if-clause.
package main
import (
"fmt"
)
func main() {
a := 19
b := &a
if b > 10 {
fmt.Println("Its bigger")
}
}
This gives error message for type mismath.
How could I successfully compare value which b is referencing. In my training code I'm reading command line arguments with flags, but I suppose this example is reprex.
How should I compare when havin only reference available?
Here b is a pointer of int means *int. You can't compare *int type with int type.
Use *b to dereference to get the value and then compare with constant value.
if *b > 10 {
fmt.Println("Its bigger")
}

Getter in GoLang [closed]

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 4 years ago.
Improve this question
I have a custom type named ProtectedCustomType and I don't want the variables within that to be set or get directly by the caller, rather want a Getter / Setter methods to do that.
Below is my ProtectedCustomType
package custom
import "fmt"
type ProtectedCustomType struct {
name string
age string
phoneNumber int
}
func (pct *ProtectedCustomType) SetAge (age string) {
pct.age=age
fmt.Println(pct.age)
}
func (pct *ProtectedCustomType) GetAge () string {
return pct.age
}
And here is my main function
package main
import (
"fmt"
"./custom"
)
var print =fmt.Println
func structCheck2() {
pct := custom.ProtectedCustomType{}
pct.SetAge("23")
age:=pct.GetAge
print (age)
}
func main() {
structCheck2()
}
I am expecting it to print 23, but it is printing as 0x48b950
This (your code) takes the pct instance's GetAge method and stores it in a variable:
age:=pct.GetAge
This calls the GetAge method and stores its return value in a variable:
age:=pct.GetAge()
Consider taking the Tour of Go and reading the Go Specification to get a basic understanding of Go syntax.

Resources