Skip to content

Instantly share code, notes, and snippets.

@noynaert
Last active April 22, 2016 03:21
Show Gist options
  • Save noynaert/785fa13b62f9c9ebdda0947fe1ebab83 to your computer and use it in GitHub Desktop.
Save noynaert/785fa13b62f9c9ebdda0947fe1ebab83 to your computer and use it in GitHub Desktop.
CSC346 course files for Json and XML
This Gist is for miscellaneous files from CSC346 at Missouri Western State University.
package main
import (
"fmt"
"encoding/json"
"log"
)
type course struct {
Discipline string
CourseNumber string
Title string `json:"course_title"`
room string `json:"boo"`
MaxEnrollment int16
}
func main() {
fmt.Println("Starting\n")
var becker course;
becker.Discipline = "CSC"
becker.CourseNumber = "410"
becker.MaxEnrollment = 25
becker.room = "A126"
becker.Title = "Computer and Network Security"
pickett := course{Discipline:"CSC", CourseNumber:"328",
Title:"Graphics", room:"A119", MaxEnrollment:25}
yan := course{"CSC", "208", "Discrete Structures","A119",20}
fmt.Println(becker)
fmt.Println(pickett)
fmt.Println(yan)
fmt.Println("-----------------------")
agenda := [3]course{yan,pickett,becker}
for i,d := range agenda{
fmt.Printf("[%v] %v\n", i, d)
}
fmt.Println("-----------------------")
data, err := json.Marshal(pickett)
if err!= nil{
log.Fatalf("Marshalling failed: %v\n",err.Error)
}
fmt.Printf("pickett as JSON: %v\n", data);
var beckerJson []byte;
beckerJson, err= json.Marshal(becker);
fmt.Printf("becker as Json: %v\n", beckerJson)
fmt.Printf("becker as Json: %s\n", beckerJson)
data,_ = json.MarshalIndent(pickett,""," ")
fmt.Printf("Pickett nicely formated: \n%s\n",data)
data,_ = json.MarshalIndent(pickett,"\u262e","------->")
fmt.Printf("Pickett nicely formated: \n%s\n",data)
data,_ = json.MarshalIndent(agenda,""," ")
fmt.Printf("The agenda slice as Json\n%s\n",data)
}
package main
import (
"fmt"
"net/url"
"net/http"
"log"
"io/ioutil"
"encoding/json"
)
type userType struct {
Login string
Id int
Avatar string `json:"avatar_url"`
GistCount int `json:"public_gists"`
}
func main() {
fmt.Println("Starting")
myURL := "https://api.github.com/users/"
//This is a simple parameter, so it isn't really necessary to encode it.
//However, it would be common to do this in other situtaions, so i included it.
query := myURL + url.QueryEscape("noynaert")
fmt.Printf("The encoded line is \"%s\"\n", query)
response, err := http.Get(query)
if err != nil {
log.Panicf("Error in http.Get: %v\n", err.Error())
}
fmt.Println("Get was successful")
fmt.Println(response)
fmt.Printf("%v\n", response.StatusCode);
//A status code of 200 means everything is OK. This is the same as a browser gets.
//It is important to realize that at this point the program is acting like a browser.
//Try misspelling noynaert and you should get a "404 missing page message, just
//like you would with a browser.
if response.StatusCode != 200 {
log.Panicf("Status code error: %v\n", response.Status)
}
//I forgot to close the response body in class, but I added it now.
//Note that it is deferred, so it will not actually close until this
//function ends.
defer response.Body.Close()
fmt.Println("This is the response.Body");
fmt.Println(response.Body)
var body []byte;
body, err = ioutil.ReadAll(response.Body)
//In GO, getting data from a web site is much like reading from a file. That is
//why the ioutil package is being used.
if err != nil {
log.Panicln("error on Reading the body: " + err.Error())
}
fmt.Println("The body slice: ");
fmt.Println(body)
fmt.Printf("%s\n", body)
var usr = new(userType)
err = json.Unmarshal(body, &usr)
if err != nil {
log.Panic(err.Error())
}
fmt.Println("The user data:")
fmt.Println("Login: " + usr.Login)
fmt.Println("Avatar URL: " + usr.Avatar)
fmt.Printf("How many gists? %v\n",usr.GistCount)
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment