my code
func HostStats() (*host.InfoStat, error) {
infoStat, err := host.Info()
fmt.Printf("All Host info: ", infoStat)
return infoStat, err
}
output
All Host info: %!(EXTRA string= {"hostname":"UDAY-PC","uptime":536323,"bootTime":1559911444,"procs":248,"os":"windows","platform":"Microsoft Windows 10 Pro","platformFamily":"Standalone Workstation","platformVersion":"10.0.17134 Build 17134","kernelVersion":"","virtualizationSystem":"","virtualizationRole":"","hostid":"0b324295-3631-47db-b6e8-83cdba2a1af9"})
I want to parse and show the below value from above:
hostname
Platform
HostId
I tried and below has the additional code:
func HostStats() (*host.InfoStat, error) {
infoStat, err := host.Info()
type Information struct {
Name string
Platform string
HostId string
}
var info []Information
info, err := json.Unmarshal(infoStat, &info)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("ok: %+v", info)
//almost every return value is a struct
fmt.Printf("All Host info: ", infoStat)
return infoStat, err
}
As Adrian mentioned above, you are having problems with your Variables.
In Go you can initialize a variable like you did:
var info string
// And assign a value to it by:
info = "foo"
The Json is unmarshaled into your info variable. The return value of the json.Unmarshal is only an error. So the correct syntax would be:
var info []Information
err := json.Unmarshal(infoStat, &info)
So remember the different ways to initialize vars and assign values to those vars.
You can look at GoDocs for Variables for more Info :)
Related
I am facing a case where I had to store dynamic values in the database with key and value pairs given by the user itself.
User gives the key and value, and I interpret it as
{"key": "user Given", "value": "user Given"}
and I add all such things to an array, and I want this array to be read into Go code where the array of objects is read from the Database table.
You can use the JSON Unmarshaler interface, but depending on how you are retrieving the data from MySql, will vary your implementation. But the idea is the same. For this example, I use https://github.com/go-sql-driver/mysql and assuming you want to store the data in an actual JSON field (Mysql >= 5.7), you could do something like this:
type MyField struct {
Key string `json:"key"`
Value string `json:"value"`
}
type MyModel struct {
ID uint64
MyFieldName MyField `json:"my_field_name"`
}
func (m *MyField) Scan(src interface{}) error {
// The data stored in a JSON field is actually returned as []uint8
val := src.([]uint8)
return json.Unmarshal(val, &m)
}
func main() {
db, err := sql.Open("mysql", "root:password#tcp(127.0.0.1)/dbname")
if err != nil {
panic(err.Error())
}
defer db.Close()
results, err := db.Query("SELECT id, my_field_name FROM my_table")
if err != nil {
panic(err.Error())
}
for results.Next() {
var row MyModel
err := results.Scan(&row.ID, &row.MyFieldName)
if err != nil {
panic(err.Error())
}
fmt.Println(row.MyFieldName.Key)
}
}
A quick hack, not necessarily the most elegant approach, is to use Golang's default JSON Unmarshaler behavior for golang maps:
jstr := `{"key": "userKeyValue", "value": "userValueValue"}`
// declare a map that has a key string and value interface{} so that any values or
// types will be accepted;
jmap := make(map[string]interface{})
err := json.Unmarshal(jstr, &jmap)
if err != nil {
log.Fatal(err)
}
for k, v := range jmap {
fmt.Printf("Key: %v, Value: %v\n", k, v)
// If you want to directly work with v remember it is an interface{}
// Thus to use it as a string you must v.(string)
}
// Will output the following:
// Key: key, Value: userKeyValue
// Key: value, Value: userValueValue
You can now use standard golang map to manipulate or manage the received data... The more elegant approach is to implement the JSON Marshaler and Unmarshaler interfaces for your declared type. These are described in the golang documentation: https://golang.org/pkg/encoding/json/
func MakeMap(w http.ResponseWriter, r *http.Request) {
// userInfo := context.Get(r, "userInfo").(model.User)
type _getData struct {
Title string `json:"title"`
Tag []string `json:"tag"`
}
var getData _getData
err := json.NewDecoder(r.Body).Decode(&getData)
if err != nil {
panic(err.Error())
}
fmt.Print(getData)
}
When I run the above code, I get the following error
2021/08/24 13:56:54 http: panic serving 127.0.0.1:50619: runtime error: invalid memory address or nil pointer dereference
goroutine 23 [running]:
net/http.(*conn).serve.func1(0x140001e9180)
/usr/local/go/src/net/http/server.go:1824 +0x108
panic(0x10505b860, 0x10522f240)
/usr/local/go/src/runtime/panic.go:971 +0x3f4
traveling/controller/mapController.MakeMap(0x1050b5630, 0x140001f40e0, 0x1400018aa00)
/Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:20 +0x3c
I've just started studying, I'm not sure why I'm having this problem, please help
err := json.NewDecoder(r.Body).Decode(&getData)
I get the following error when i change code line 20 like above
2021/08/24 14:16:44 http: panic serving 127.0.0.1:51396: invalid character '-' in numeric literal
goroutine 23 [running]:
net/http.(*conn).serve.func1(0x140001e9360)
/usr/local/go/src/net/http/server.go:1824 +0x108
panic(0x100d85d00, 0x14000206070)
/usr/local/go/src/runtime/panic.go:971 +0x3f4
traveling/controller/mapController.MakeMap(0x100df1630, 0x140001f40e0, 0x1400018aa00)
/Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:24 +0x194
net/http.HandlerFunc.ServeHTTP(0x100de75d8, 0x100df1630, 0x140001f40e0, 0x1400018aa00)
/usr/local/go/src/net/http/server.go:2069 +0x40
To get the multipart form data from a POST/PUT/PATCH request's body you can use the ParseMultipartForm method to parse the body and then access the data through the PostForm field. Or you can use FormValue to get just the first value associated with the form's field.
maxMemory := 32<<20
if err := r.ParseMultipartForm(maxMemory); err != nil {
panic(err)
}
fmt.Println(_getData{
Title: r.FormValue("title"), // FormValue returns string
Tag: r.PostForm["tag[]"], // PostForm is a map of []string
})
You can use to parse form data into json like annotated struct using package github.com/senpathi/paramex. Struct fields must be annotated with param keyword and tag name is key of the form data.
your struct should be as below.
type _getData struct {
Title string `param:"title"`
Tag []string `param:"tag[]"`
}
This is the updated MakeMap handler function for your postman request mentioned in the question
func MakeMap(w http.ResponseWriter, r *http.Request) {
// userInfo := context.Get(r, "userInfo").(model.User)
type _getData struct {
Title string `param:"title"`
Tag []string `param:"tag[]"`
}
// this needed because u send data from Postman as multipart/form-data
maxMemory := 32<<20
if err := r.ParseMultipartForm(int64(maxMemory)); err != nil {
panic(err)
}
var getData _getData
extractor := paramex.NewParamExtractor()
err := extractor.ExtractForms(&getData, r)
if err != nil {
panic(err.Error())
}
fmt.Print(getData)
//Output: {defaultMap [travelling travelling2]}
}
I need to parse really long json file (more than million items). I don't want to load it to the memory and read it chunk by chunk. There's a good example with the array of items here. The problem is that I deal with the map. And when I call Decode I get not at beginning of value.
I can't get what should be changed.
const data = `{
"object1": {"name": "cattle","location": "kitchen"},
"object2": {"name": "table","location": "office"}
}`
type ReadObject struct {
Name string `json:"name"`
Location string `json:"location"`
}
func ParseJSON() {
dec := json.NewDecoder(strings.NewReader(data))
tkn, err := dec.Token()
if err != nil {
log.Fatalf("failed to read opening token: %v", err)
}
fmt.Printf("opening token: %v\n", tkn)
objects := make(map[string]*ReadObject)
for dec.More() {
var nextSymbol string
if err := dec.Decode(&nextSymbol); err != nil {
log.Fatalf("failed to parse next symbol: %v", err)
}
nextObject := &ReadObject{}
if err := dec.Decode(&nextObject); err != nil {
log.Fatalf("failed to parse next object")
}
objects[nextSymbol] = nextObject
}
tkn, err = dec.Token()
if err != nil {
log.Fatalf("failed to read closing token: %v", err)
}
fmt.Printf("closing token: %v\n", tkn)
fmt.Printf("OBJECTS: \n%v\n", objects)
}
TL,DR: when you are calling Token() method for a first time, you move offset from the beginning (of a JSON value) and therefore you get the error.
You are working with this struct (link):
type Decoder struct {
// others fields omits for simplicity
tokenState int
}
Pay attention for a tokenState field. This value could be one of (link):
const (
tokenTopValue = iota
tokenArrayStart
tokenArrayValue
tokenArrayComma
tokenObjectStart
tokenObjectKey
tokenObjectColon
tokenObjectValue
tokenObjectComma
)
Let's back to your code. You are calling Token() method. This method obtains first JSON-valid token { and changes tokenState from tokenObjectValue to the tokenObjectStart (link). Now you are "in-an-object" state.
If you try to call Decode() at this point you will get an error (not at beginning of value). This is because allowed states of tokenState for calling Decode() are tokenTopValue, tokenArrayStart, tokenArrayValue, tokenObjectValue, i.e. "full" value, not part of it (link).
To avoid this you can just don't call Token() at all and do something like this:
dec := json.NewDecoder(strings.NewReader(dataMapFromJson))
objects := make(map[string]*ReadObject)
if err := dec.Decode(&objects); err != nil {
log.Fatalf("failed to parse next symbol: %v", err)
}
fmt.Printf("OBJECTS: \n%v\n", objects)
Or, if you want to read chunk-by-chunk, you could keep calling Token() until you reach "full" value. And then call Decode() on this value (I guess this should work).
After consuming the initial { with your first call to dec.Token(), you must :
use dec.Token() to extract the next key
after extracting the key, you can call dec.Decode(&nextObject) to decode an entry
example code :
for dec.More() {
key, err := dec.Token()
if err != nil {
// handle error
}
var val interface{}
err = dec.Decode(&val)
if err != nil {
// handle error
}
fmt.Printf(" %s : %v\n", key, val)
}
https://play.golang.org/p/5r1d8MsNlKb
I need to get one param from posted json.
And I don't want to make struct for only this.
This is what I have tried
type NewTask struct {
Price uint64 `json:"price"`
}
func (pc TaskController) Create(c *gin.Context) {
var service Service
if err := c.BindJSON(&service); err != nil {
log.Println(err) // this works
}
var u NewTask
if err := c.BindJSON(&u); err != nil {
log.Println(err) // this return EOF error
}
fmt.Println(u.Price)
}
Requested Json data have many other fields including price
{
...other fields
price: 30
}
But this don't work.I think its because I am binding twice, How can I success in binding multiple?
Thanks
Try to use ShouldBindJSON. The BindJSON is reading the body, so we are at EOF if the context Body get read multiple times.
ShouldBindJSON stores the request body into the context, and reuse when it is called again.
Below is the whole code of the program. It is a service that forwards requests. Currently working. What I am trying to do is get rid of the yml file that is currently storing all configs and move them to db. I don't want to mess with the code much, so my idea was to simply store the db data in the same structs.
// Config contains configuration for this service
type Instance struct {
User string `json:"user"`
Password string `json:"password"`
InstanceId string `json:"instance_id"`
InstanceType string `json:"instance_type"`
InstanceMode string `json:"instance_mode"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
PublicKey string `json:"pubkey"`
Apis Api `json:"apis"`
}
// API struct
type Api struct {
Name string `json:"name"`
Url string `json:"url"`
Version string `json:"version"`
IsBhdGw bool `json:"isBhdGw"`
Key string `json:"key"`
}
// API struc
type InfoResponse struct {
InstanceId string `json:"instance_id"`
InstanceType string `json:"instance_type"`
InstanceMode string `json:"instance_mode"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
PublicKey string `json:"pubkey"`
Apis Api `json:"apis"`
}
type Settings struct {
Port int `json:"port"`
User string `json:"user"`
Secret string `json:"secret"`
Mode string `json:"mode"`
}
type Instances struct {
Instances []Instance
}
var payloadLength int = 3
var instances Instances
var settings Settings
var db *sql.DB
func fetchInstances() []Instance {
rows, err := db.Query("SELECT Instances.InstanceId, Instances.User, Instances.Password, Instances.InstanceType, Instances.InstanceMode, Instances.ClientId, Instances.ClientSecret, Instances.PublicKey, Api.Name, Api.Url, Api.Version, Api.IsBhdGw, Api.Key FROM Instances, Api")
if err != nil {
panic(err.Error())
}
defer rows.Close()
instances := []Instance{}
for rows.Next() {
var instance Instance
var apis Api
err := rows.Scan(&instance.InstanceId, &instance.User, &instance.Password,
&instance.InstanceType, &instance.InstanceMode, &instance.ClientId, &instance.ClientSecret, &instance.PublicKey,
&apis.Name, &apis.Url, &apis.Version, &apis.IsBhdGw, &apis.Key)
if err != nil {
panic(err.Error())
}
instance.Apis = apis
instances = append(instances, instance)
}
return instances
}
When I run this I get a "panic: runtime error: invalid memory address or nil pointer dereference"
You are attempting to connect to an uninitiated database connection. This is due to variable shadowing.
Although you've defined a global db variable:
var db *sql.DB
Your main() method creates a new one when it connects:
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"#tcp(db:3306)/"+dbName)
So then when you call fetchInstances, which uses the global variable, the global one is still unset.
The easiest solution is to change your code in main() as follows:
var err error
db, err = sql.Open(dbDriver, dbUser+":"+dbPass+"#tcp(db:3306)/"+dbName)
But the better solution is to never use a global variable. Use the locally-defined variable in main, then pass that to fetchInstances.
As a side note: ALWAYS, ALWAYS check your errors. Twice in your code, you fail to check db connection errors, as in:
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"#tcp(db:3306)/"+dbName)
err = db.Ping()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
If the connection fails, your db.Ping() call will likely panic. Instead, you should do:
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"#tcp(db:3306)/"+dbName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = db.Ping()
if err != nil {
fmt.Println(err)
os.Exit(1)
}