I have a Go variable of type string, when I do fmt.Println(variable) my terminal prints out
{"access_key":"AAAAAA","secret_key":"XXXXXXX"}
. How do I extract the values "AAAAAA" and "XXXXXXX"?
This is an example of working code
package main
import (
"encoding/json"
"fmt"
)
type Somestruct struct {
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
}
func main() {
var output Somestruct
S := `{"access_key":"AAAAAA","secret_key":"XXXXXXX"}`
json.Unmarshal([]byte(S), &output)
fmt.Println(output.SecretKey)
}
See https://play.golang.org/p/vaPVHmo8fhR
I suggested that you learn how to work with json and structs in go.
https://golang.org/pkg/encoding/json/
Related
I would like to know if it is possible to get the name of a property from a structure and convert it to string.
For example in the following code:
package main
import "fmt"
type StructA struct {
ValueAA string
ValueAB string
}
type StructB struct {
ValueBA string
ValueBB string
RefStructA StructA
}
func main() {
//pass any attribute of any structure
fmt.Println(castProperty(StructB.RefStructA.ValueAA))
//print the name passed but in string. Do not print the value
//expected output: "StructB.RefStructA.ValueAA"
}
func castProperty(value interface{}) string {
//some code
}
Is it possible to write a function that allows obtaining the name of the property of a structure and converted to a string? property value is not required.
That's called Reflection. I let you read the page, it lets you do what you want.
First, read the The first law of reflection: https://go.dev/blog/laws-of-reflection
package main
import (
"fmt"
"reflect"
)
func main() {
var x float64 = 3.4
fmt.Println("type:", reflect.TypeOf(x))
}
https://play.golang.org/p/OuGgD1TlSMO
I am not sure exactly how do you want to give input to your function but here is an example that may help you
package main
import (
"log"
"reflect"
)
func main() {
getPropertyName(B{})
}
type A struct {
field1 string
}
type B struct {
field A
}
func getPropertyName(b interface{}) {
parentType := reflect.TypeOf(b)
val := reflect.ValueOf(b)
for i := 0; i< val.Type().NumField(); i++ {
t := val.Type().Field(i)
ty := val.Type().Field(i).Type.Name()
log.Println(parentType.Name()+"."+ t.Name+"."+ty)
}
}
you could do something like this :
package main
import (
"fmt"
)
type StructA struct {
ValueAA string
ValueAB string
}
type StructB struct {
ValueBA string
ValueBB string
RefStructA StructA
}
func main() {
x := &StructB{RefStructA: StructA{ValueAA: "something"}}
fmt.Printf("%+v", x)
}
out :
&{ValueBA: ValueBB: RefStructA:{ValueAA:something ValueAB:}}
I'm new to Golang and this could be so basic. Below is an example demonstrating my need. Junk should accept only id and name. Even though unmarshal eliminates extras, i need a way to hard stop execution stating json contain invalid key value pair. Could you please let me know if there's any.
package main
import (
"fmt"
"encoding/json"
)
type Junk struct{
ID int `json:"id"`
Name string `json:"name"`
Area string `json:"area"`
}
func main() {
a:=Junk{}
data:= `{"id":1,"name":"gg","junk":"Junk value"}`
err := json.Unmarshal([]byte(data),&a)
if err!=nil{
fmt.Println(err)
}
fmt.Println(a)
}
Use func (*Decoder) DisallowUnknownFields():
DisallowUnknownFields causes the Decoder to return an error when the
destination is a struct and the input contains object keys which do
not match any non-ignored, exported fields in the destination.
package main
import (
"encoding/json"
"fmt"
"strings"
)
type Junk struct {
ID int `json:"id"`
Name string `json:"name"`
Area string `json:"area"`
}
func main() {
a := Junk{}
data := `{"id":1,"name":"gg","junk":"Junk value"}`
d := json.NewDecoder(strings.NewReader(data))
d.DisallowUnknownFields()
if err := d.Decode(&a); err != nil {
fmt.Println(err)
}
fmt.Println(a)
}
Try it out: https://play.golang.org/p/aTj2C-AAuZ7
This is my code:
package main
import (
"fmt"
)
type person struct {
//name [][]string{};
name [][]string
}
func main() {
var people = map[string]*person{}
people["first person"] = &person{name:{{"My name","30"}}}
fmt.Println(people["first person"])
}
I have an error:
missing type in composite literal
I want output as [[My name,30]]
Could someone help me?
Here is working example. You must declare type of composed literal before using.
package main
import (
"fmt"
)
type person struct {
//name [][]string{};
name [][]string
}
func main() {
var people = map[string]*person{}
people["first person"] = &person{name: [][]string{{"John", "30"}}}
fmt.Println(people["first person"])
}
You are missing type while creating an instance pointer and initializing it, it should be:
&person{name: [][]string{{"My name, 30"}}}
Below is the working example:
package main
import (
"fmt"
)
type person struct {
name [][]string
}
func main() {
var people = map[string]*person{}
people["first person"] = &person{name: [][]string{{"My name, 30"}}}
fmt.Println(people["first person"].name)
}
I was testing golang functionalities and came across this concept where I can use a pointer of an interface as an interface itself. In the below code, how do I ensure that the value of one changes to random.
package main
import (
"fmt"
)
func some(check interface{}) {
check = "random"
}
func main() {
var one *interface{}
some(one)
fmt.Println(one)
}
Specifically, I need ways in which I can pass an interface pointer to a function which accepts an interface as an argument.
Thanks!
Accept a pointer to interface{} as the first parameter to some
Pass the address of one to some
package main
import (
"fmt"
)
func some(check *interface{}) {
*check = "random"
}
func main() {
var one interface{}
some(&one)
fmt.Println(one)
}
https://play.golang.org/p/ksz6d4p2f0
If you want to keep the same signature of some, you will have to use the reflect package to set the interface{} pointer value:
package main
import (
"fmt"
"reflect"
)
func some(check interface{}) {
val := reflect.ValueOf(check)
if val.Kind() != reflect.Ptr {
panic("some: check must be a pointer")
}
val.Elem().Set(reflect.ValueOf("random"))
}
func main() {
var one interface{}
some(&one)
fmt.Println(one)
}
https://play.golang.org/p/ocqkeLdFLu
Note: val.Elem().Set() will panic if the value passed is not assignable to check's pointed-to type.
If I have a struct which I want to be able to Marhsal/Unmarshal things in and out of xml with (using encoding/xml) - how can I not print attributes which are empty?
package main
import (
"encoding/xml"
"fmt"
)
type MyThing struct {
XMLName xml.Name `xml:"body"`
Name string `xml:"name,attr"`
Street string `xml:"street,attr"`
}
func main() {
var thing *MyThing = &MyThing{Name: "Canister"}
result, _ := xml.Marshal(thing)
fmt.Println(string(result))
}
For example see http://play.golang.org/p/K9zFsuL1Cw
In the above playground I'd not want to write out my empty street attribute; how could I do that?
Use omitempty flag on street field.
From Go XML package:
a field with a tag including the "omitempty" option is omitted
if the field value is empty. The empty values are false, 0, any
nil pointer or interface value, and any array, slice, map, or
string of length zero.
In case of your example:
package main
import (
"encoding/xml"
"fmt"
)
type MyThing struct {
XMLName xml.Name `xml:"body"`
Name string `xml:"name,attr"`
Street string `xml:"street,attr,omitempty"`
}
func main() {
var thing *MyThing = &MyThing{Name: "Canister"}
result, _ := xml.Marshal(thing)
fmt.Println(string(result))
}
Playground