Created
October 24, 2016 20:00
-
-
Save hsleonis/61efc7cd234906cf107b1cac4bc2e002 to your computer and use it in GitHub Desktop.
HTTP request on GoLang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
//"os" | |
"encoding/csv" | |
"fmt" | |
"strconv" | |
"sort" | |
"time" | |
"net/http" | |
"log" | |
) | |
func main(){ | |
//f, err := os.Open("./Environmental_Data_Deep_Moor_2015.txt") | |
res, err := http.Get("https://raw.githubusercontent.com/lyndadotcom/LPO_weatherdata/master/Environmental_Data_Deep_Moor_2015.txt") | |
if err!=nil{ | |
log.Fatal(err) | |
} | |
//defer f.Close() | |
rdr := csv.NewReader(res.Body) | |
rdr.Comma = '\t' | |
rdr.TrimLeadingSpace = true | |
defer res.Body.Close() | |
rows, err := rdr.ReadAll() | |
if err!=nil{ | |
panic(err) | |
} | |
start:=time.Now() | |
fmt.Println("Total record:", len(rows)-1) | |
if len(rows)-1>0{ | |
fmt.Println("Mean Air Temparature:", mean(rows, 1)) | |
fmt.Println("Mean Barometric pressure:", mean(rows, 2)) | |
fmt.Println("Mean Wind Speed", mean(rows, 7)) | |
fmt.Println("Median Air Temparature:", median(rows, 1)) | |
fmt.Println("Median Barometric pressure:", median(rows, 2)) | |
fmt.Println("Median Wind Speed", median(rows, 7)) | |
} | |
end:=time.Now() | |
fmt.Println(end.Sub(start)) | |
} | |
func mean(rows [][]string, idx int) float64{ | |
var total float64 | |
for i,row := range rows{ | |
if i!=0{ | |
val, _ := strconv.ParseFloat(row[idx],64) | |
total+=val | |
} | |
} | |
return total/ float64(len(rows)-1) | |
} | |
func median(rows [][]string, idx int) float64{ | |
var sorted[] float64 | |
for i,row := range rows{ | |
if i!=0{ | |
val, _ := strconv.ParseFloat(row[idx],64) | |
sorted = append(sorted, val) | |
} | |
} | |
sort.Float64s(sorted) | |
middle := len(rows)/2 | |
if len(rows)%2==0{ | |
high := sorted[middle] | |
low := sorted[middle-1] | |
return (high+low)/2 | |
} | |
return sorted[middle]/2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment