Skip to content

Instantly share code, notes, and snippets.

@stupidbodo
Last active February 12, 2024 07:05
Show Gist options
  • Save stupidbodo/71f2b164744a18a18e74 to your computer and use it in GitHub Desktop.
Save stupidbodo/71f2b164744a18a18e74 to your computer and use it in GitHub Desktop.
Golang - Read CSV/JSON from URL
package main
import (
"encoding/csv"
"fmt"
"net/http"
)
func readCSVFromUrl(url string) ([][]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
reader.Comma = ';'
data, err := reader.ReadAll()
if err != nil {
return nil, err
}
return data, nil
}
func main() {
url := "https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.csv"
data, err := readCSVFromUrl(url)
if err != nil {
panic(err)
}
for idx, row := range data {
// skip header
if idx == 0 {
continue
}
if idx == 6 {
break
}
fmt.Println(row[2])
}
}
// Will Print:
// AF
// AX
// AL
// DZ
// AS
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type Country struct {
CountryName CountryName `json:"name"`
TLD []string `json:"tld"`
CCA2 string `json:"cca2"`
CCN3 string `json:"ccn3"`
CCA3 string `json:"cca3"`
Currency []string `json:"currency"`
CallingCode []string `json:"callingCode"`
Capital string `json:"capital"`
AlternateSpellings []string `json:"altSpellings"`
Relevance string `json:"relevance"`
Region string `json:"region"`
Subregion string `json:"subregion"`
NativeLanguage string `json:"nativeLanguage"`
Languages map[string]string `json:"languages"`
Translations map[string]string `json:"translations"`
LatLng [2]float64 `json:"latlng"`
Demonym string `json:"demonym"`
Borders []string `json:"borders"`
Area float64 `json:"area"`
}
type CountryName struct {
Common string `json:"common"`
Official string `json:"official"`
Native CountryNameNative `json:"native"`
}
type CountryNameNative struct {
Common string `json:"common"`
Official string `json:"official"`
}
func readJSONFromUrl(url string) ([]Country, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var countryList []Country
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
respByte := buf.Bytes()
if err := json.Unmarshal(respByte, &countryList); err != nil {
return nil, err
}
return countryList, nil
}
func main() {
url := "https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json"
countryList, err := readJSONFromUrl(url)
if err != nil {
panic(err)
}
for idx, row := range countryList {
// skip header
if idx == 0 {
continue
}
if idx == 6 {
break
}
fmt.Println(row.CountryName.Common)
}
}
// Will Print:
// Åland Islands
// Albania
// Algeria
// American Samoa
// Andorra
@florianjs
Copy link

Thank you

@inthecatsdreams
Copy link

Very useful, thank you!

@JovaniTarnowski
Copy link

Very useful, thank you!²

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment