Created
April 23, 2018 01:05
-
-
Save deepumi/f2dfd50c6f65c97ccce2365f6f74aaff to your computer and use it in GitHub Desktop.
Go samples
This file contains 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
//main.go | |
package main | |
import ( | |
"fmt" | |
"time" | |
"awesomeProject/another" | |
) | |
func Index(vs []string, t string) int { | |
for i, v := range vs { | |
fmt.Println(v) | |
if v == t { | |
return i | |
} | |
} | |
return -1 | |
} | |
func Any(vs []string, f func(string) bool) bool { | |
for _, v := range vs { | |
if f(v) { | |
return true | |
} | |
} | |
return false | |
} | |
type AutoGenerated []struct { | |
Name string `json:"Name"` | |
ExpiryDate time.Time `json:"ExpiryDate"` | |
Price float64 `json:"Price"` | |
} | |
type opt struct { | |
shortnm byte | |
longnm string | |
needArg bool | |
} | |
func main() { | |
/*var strs = []string{"peach", "apple", "pear", "plum"} | |
var start = time.Now() | |
fmt.Println("Helo world %s", start) | |
fmt.Println(another.Foobar(2)) | |
elapsed := start.Sub(start) | |
fmt.Println(elapsed) | |
fmt.Println(Index(strs, "pear")) | |
var s = Any(strs, func(v string) bool { | |
return strings.HasPrefix(v, "p") | |
}) | |
fmt.Println(s) | |
//s1 := make([]AutoGenerated, 1) | |
//s1[0] = | |
var basenameOpts = []opt { | |
{ | |
shortnm: 'a', | |
longnm: "multiple", | |
needArg: false, | |
}, | |
{ | |
shortnm: 'b', | |
longnm: "b-option", | |
needArg: false, | |
}, | |
} | |
var basenameOpts1 = []opt{} | |
for i := 0; i < 47; i++ { | |
n := opt{longnm: "F "} | |
basenameOpts1 = append(basenameOpts1,n) | |
} | |
for i, _ := range basenameOpts { | |
basenameOpts[i].longnm = basenameOpts[i].longnm + " something" | |
fmt.Println(basenameOpts[i].longnm,basenameOpts[i].shortnm) | |
}*/ | |
another.GetData() | |
another.DictionaryMap() | |
} | |
//https://gobyexample.com/collection-functions | |
//https://mholt.github.io/json-to-go/ | |
//https://gobyexample.com/slices | |
//https://blog.golang.org/json-and-go | |
//https://www.tutorialspoint.com/go/go_function_as_values.htm | |
//https://www.dotnetperls.com/container-list-go | |
//https://blog.alexellis.io/golang-json-api-client/ | |
//another.go | |
package another //create folder called another/another.go | |
import ( | |
"net/http" | |
"time" | |
"log" | |
"io/ioutil" | |
"encoding/json" | |
"fmt" | |
) | |
type people1 struct { | |
Number int `json:"number"` | |
Message string `json:"Message"` | |
} | |
type AutoGeneratedPerson struct { | |
People []struct { | |
Name string `json:"name"` | |
Craft string `json:"craft"` | |
} `json:"people"` | |
Number int `json:"number"` | |
Message string `json:"message"` | |
} | |
func Foobar(i int) int { | |
return i + 1; | |
} | |
/* | |
Get Some json data from a URL and deserialize to object and print | |
*/ | |
func GetData(){ | |
url := "http://api.open-notify.org/astros.json" | |
httpClient := http.Client{ | |
Timeout: time.Second * 2, // Maximum of 2 secs | |
} | |
req, error := http.NewRequest(http.MethodGet, url, nil) | |
if error != nil { | |
log.Fatal(error) | |
} | |
req.Header.Set("User-Agent", "spacecount-tutorial") | |
res, error := httpClient.Do(req) | |
if error != nil { | |
log.Fatal(error) | |
} | |
body, error := ioutil.ReadAll(res.Body) | |
if error != nil { | |
log.Fatal(error) | |
} | |
personList := AutoGeneratedPerson{} | |
jsonErr := json.Unmarshal(body, &personList) | |
if jsonErr != nil { | |
log.Fatal(jsonErr) | |
} | |
fmt.Println(personList.Message) | |
for _,p := range personList.People { | |
fmt.Println(p.Name, p.Craft) | |
} | |
} | |
func DictionaryMap() { //https://blog.golang.org/go-maps-in-action | |
var m map[string]int = make(map[string]int) | |
m["route"] = 70 | |
m["route"] = 78 | |
for key, value := range m { | |
fmt.Println("Key:", key, "Value:", value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment