How can I fix the error?
http://play.golang.org/p/0UMnUZOUHw
// JSON to CSV in Golang
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Json struct {
RecordID int64 `json:"recordId"`
DOJ string `json:"Date of joining"`
EmpID string `json:"Employee ID"`
}
func main() {
// reading data from JSON File
data, err := ioutil.ReadFile("./people.json")
if err != nil {
fmt.Println(err)
}
// Unmarshal JSON data
var d []Json
err = json.Unmarshal([]byte(data), &d)
if err != nil {
fmt.Println(err)
}
// Create a csv file
f, _ := os.Create("./people.csv")
defer f.Close()
// Write Unmarshaled json data to CSV file
w := csv.NewWriter(f)
// How to proceed further?
/* I am getting errors if i use below
for _,obj := range d {
var record []interface{}
record = append(record, obj.RecordID)
record = append(record, obj.DOJ)
record = append(record, obj.EmpID)
w.Write(record)
}
*/
}
Error:
cannot use record (type []interface {}) as type []string in function argument
For example,
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type Json struct {
RecordID int64 `json:"recordId"`
DOJ string `json:"Date of joining"`
EmpID string `json:"Employee ID"`
}
func main() {
// reading data from JSON File
data, err := ioutil.ReadFile("./people.json")
if err != nil {
fmt.Println(err)
}
// Unmarshal JSON data
var d []Json
err = json.Unmarshal([]byte(data), &d)
if err != nil {
fmt.Println(err)
}
// Create a csv file
f, err := os.Create("./people.csv")
if err != nil {
fmt.Println(err)
}
defer f.Close()
// Write Unmarshaled json data to CSV file
w := csv.NewWriter(f)
for _, obj := range d {
var record []string
record = append(record, strconv.FormatInt(obj.RecordID, 10))
record = append(record, obj.DOJ)
record = append(record, obj.EmpID)
w.Write(record)
}
w.Flush()
}
csv Writer's Write does not expect an []interface{} it expects a []string.
Related
If I receive from an API a string obeying the format of ".01", and I have a struct like this:
type Mystruct struct {
Val float64 json:"val,string"
}
In this case, I receive trying to unmarshal val into float64. Is there a way I can accomplish this?
Add a string field to capture the string value:
type Mystruct struct {
Val float64 `json:"-"`
XVal string `json:"val"`
}
Unmarshal the JSON document. Convert the string value to a float value:
var v Mystruct
err := json.Unmarshal([]byte(data), &v)
if err != nil {
log.Fatal(err)
}
v.Val, err = strconv.ParseFloat(v.XVal, 64)
if err != nil {
log.Fatal(err)
}
I recommand defining a type alias which you can use it anywhere.
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
type MyFloat64 float64
func (f *MyFloat64) UnmarshalJSON(data []byte) error {
raw := string(data)
raw = strings.TrimPrefix(raw, "\"")
raw = strings.TrimSuffix(raw, "\"")
if parsedFloat, err := strconv.ParseFloat(raw, 64); err != nil {
return err
} else {
*f = MyFloat64(parsedFloat)
return nil
}
}
type MyObj struct {
Val1 MyFloat64
Val2 string
}
func main() {
j := `{"Val1":"0.01", "Val2":"0.01"}`
o := MyObj{}
err := json.Unmarshal([]byte(j), &o)
if err != nil {
fmt.Println(err)
} else {
b, _ := json.Marshal(o)
fmt.Println("in:", j)
fmt.Println("out:", string(b))
}
}
output:
in: {"Val1":"0.01", "Val2":"0.01"}
out: {"Val1":0.01,"Val2":"0.01"}
I have a data frame
import (
"context"
"errors"
"fmt"
dataframe "github.com/rocketlaunchr/dataframe-go"
"io"
"log"
"net/http"
"os"
"time"
"github.com/rocketlaunchr/dataframe-go/imports"
)
the outside a main function i have
func readFile(fileName string) *Return datatype* {
covidData, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
dataFrame, err := imports.LoadFromCSV(ctx, covidData)
if err != nil {
log.Fatal(err)
}
fmt.Println("Read file CSV")
return (dataFrame)
}
I'm needing to return the dataFrame array to the main function, I don't know if this I have to doit with *pointers.
Also in the main func I have to initialize that dataframe.
Edit 1:
i add the return value to dataframe.DataFrame
func readFile(fileName string) *dataframe.DataFrame {
covidData, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
dataFrame, err := imports.LoadFromCSV(ctx, covidData)
if err != nil {
log.Fatal(err)
}
fmt.Println("Read file CSV")
// fmt.Println("Printing data Frame")
// Print The whole table
//fmt.Println(dataFrame)
fmt.Println("dataFrame", reflect.TypeOf(dataFrame))
prnNumLst(dataFrame.Names())
return dataFrame
}
seems to work fine thanks to all
TL;DR
The return type of func readFile(fileName string) should be *dataframe.DataFrame.
Reasoning
You use the imports.LoadFromCSV(...) function to load your CSV file and want to return the resulting data frame. The API documentation of the github.com/rocketlaunchr/dataframe-go reveals, that LoadFromCSV function a pointer to your dataframe: *dataframe.DataFrame.
See documentation here documentation of LoadFromCSV:
func LoadFromCSV(ctx context.Context, r io.ReadSeeker, options ...CSVLoadOptions) (*dataframe.DataFrame, error)\
LoadFromCSV will load data from a csv file.
I have this JSON API that returns the delivery date as a UNIX timestamp. I would prefer using Time in the rest of the application. The following works but doesn't feel very Go like.
type Delivery struct {
Time string `json:"time"`
}
func timestampToTime(s string) time.Time {
i, _ := strconv.ParseInt(s, 10, 64)
returns time.Unix(i, 0)
}
fmt.Println(timestampToTime(Delivery.Time))
// 2019-02-17 11:55:00 +0100 CET
Is there a way to cast an incoming value in a struct?
You can do something very similar to the custom JSON Unmarshal method described here:
http://choly.ca/post/go-json-marshalling/
Assuming that the JSON contains a string in your case this would look like this:
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"
)
const input = `{"time": "946684799"}`
type Delivery struct {
Time time.Time `json:"time"`
}
func (d *Delivery) UnmarshalJSON(data []byte) error {
type Alias Delivery
aux := &struct {
Time string `json:"time"`
*Alias
}{
Alias: (*Alias)(d),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i, err := strconv.ParseInt(aux.Time, 10, 64)
if err != nil {
return err
}
d.Time = time.Unix(i, 0)
return nil
}
func main() {
var delivery Delivery
err := json.Unmarshal([]byte(input), &delivery)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(delivery.Time)
}
https://play.golang.org/p/mdOmUO2EDIR
YAML files can contain fields with "multi-line string" data. Example below:
Data:
Foo: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
How can I properly encode and decode these in Golang and what should be the type of the Data field, map[string][]byte?
I tried:
import (
yamlv2 "gopkg.in/yaml.v2"
)
type data struct {
Data map[string][]byte
}
func decode(bytes []byte) (*data, error) {
d := &data{}
err := yamlv2.Unmarshal(bytes, d)
if err != nil {
return nil, err
}
return d, nil
}
Also see in Playground.
You would parse a multi-line string the same way as a normal string:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
)
var yamlString = `
Data:
Foo: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten`
type data struct {
Data map[string]string `yaml:"Data"`
}
func main() {
t := data{}
err := yaml.Unmarshal([]byte(yamlString), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
if foo, ok := t.Data["Foo"]; ok {
fmt.Printf("--- t:\n%v\n\n", foo)
}
}
I have a Go template that should resolve to a struct. How can I convert the bytes.Bufferresult from template execute function back to the struct. Playground
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"text/template"
)
type Data struct {
Age int
Username string
SubData SubData
}
type SubData struct {
Name string
}
func main() {
s := SubData{Name: "J. Jr"}
d := Data{Age: 26, Username: "HelloWorld", SubData: s}
tmpl := "{{ .SubData }}"
t := template.New("My template")
t, _ = t.Parse(string(tmpl))
buffer := new(bytes.Buffer)
t.Execute(buffer, d)
fmt.Println(buffer)
// writing
enc := gob.NewEncoder(buffer)
err := enc.Encode(s)
if err != nil {
log.Fatal("encode error:", err)
}
// reading
buffer = bytes.NewBuffer(buffer.Bytes())
e := new(SubData)
dec := gob.NewDecoder(buffer)
err = dec.Decode(e)
if err != nil {
log.Fatal("decode error:", err)
}
fmt.Println(e, err)
}
You cannot. This is plain simply impossible.
But why on earth would anybody want to do something like this? Why don't you just send your Data directly via gob and decode it directly? Why creating a textual representation which you gob?