methods and json parse
This commit is contained in:
parent
2d5291d057
commit
55704f8365
127
method.go
Normal file
127
method.go
Normal file
@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "strings"
|
||||
import "math"
|
||||
|
||||
|
||||
type MyName string
|
||||
|
||||
func printName(name MyName) {
|
||||
fmt.Println(name)
|
||||
}
|
||||
|
||||
func camelCase(name MyName) {
|
||||
result := strings.Split(string(name), " ")
|
||||
|
||||
//fmt.Println(result[0][0])
|
||||
|
||||
firstName := strings.ToUpper(string(result[0][0])) + result[0][1:]
|
||||
lastName := strings.ToUpper(string(result[1][0])) + result[1][1:]
|
||||
|
||||
fmt.Printf("First Name: %s\nLast Name: %s", firstName, lastName)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
type Rectangle struct {
|
||||
width float64
|
||||
height float64
|
||||
name string
|
||||
}
|
||||
|
||||
type Triangle struct {
|
||||
height float64
|
||||
base float64
|
||||
name string
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float64
|
||||
circleName string
|
||||
}
|
||||
|
||||
|
||||
func (c *Circle) Area() float64 {
|
||||
return math.Pow(c.radius, 2) * math.Pi
|
||||
}
|
||||
|
||||
func (c *Circle) Name() string {
|
||||
return c.circleName
|
||||
}
|
||||
|
||||
|
||||
func (t *Triangle) Area() float64 {
|
||||
return ( t.height * t.base ) / 2
|
||||
}
|
||||
|
||||
func (t *Triangle) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (r *Rectangle) Name() string {
|
||||
return r.name
|
||||
}
|
||||
|
||||
func (r *Rectangle) Area() float64 {
|
||||
return r.width * r.height }
|
||||
|
||||
func (r *Rectangle) Scale(amount float64) {
|
||||
r.width = r.width * amount
|
||||
r.height = r.height * amount
|
||||
}
|
||||
|
||||
type Geometric interface {
|
||||
Area() float64
|
||||
Name() string
|
||||
}
|
||||
|
||||
type myInt int
|
||||
|
||||
func (i *myInt) Area() float64 {
|
||||
return 0.5
|
||||
}
|
||||
|
||||
func (i *myInt) Name() string {
|
||||
return "I am an int"
|
||||
}
|
||||
|
||||
|
||||
func printArea(g []Geometric) {
|
||||
for _, obj := range(g) {
|
||||
fmt.Printf("%v ---> Area: %v\n", obj.Name(), obj.Area())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
|
||||
r := Rectangle{2,5, "rectangle"}
|
||||
t := Triangle{3, 5, "triangle"}
|
||||
c := Circle{2, "circle"}
|
||||
var i myInt = 4
|
||||
|
||||
g := []Geometric{&r, &t, &c, &i}
|
||||
|
||||
|
||||
printArea(g)
|
||||
|
||||
|
||||
|
||||
//fmt.Printf("Rectangle: %v ---> Area: %v\n", r, r.Area())
|
||||
//fmt.Printf("Triangle: %v ---> Area: %v\n", t, t.Area())
|
||||
//fmt.Printf("Circle: %v ---> Area: %v\n", c, c.Area())
|
||||
|
||||
//g = &r
|
||||
//printArea(g)
|
||||
|
||||
//g = &t
|
||||
//printArea(g)
|
||||
|
||||
//g = &c
|
||||
//printArea(g)
|
||||
|
||||
}
|
45
parsejson.go
Normal file
45
parsejson.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "io/ioutil"
|
||||
import "encoding/json"
|
||||
|
||||
|
||||
type Person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Name string
|
||||
Url string
|
||||
Tags []string `json:"categories"`
|
||||
People []Person
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
|
||||
f, _ := ioutil.ReadFile("test2.json")
|
||||
|
||||
//var data Data // does not allocate memory (must use make)
|
||||
|
||||
data := Data{}
|
||||
|
||||
_ = json.Unmarshal(f, &data)
|
||||
|
||||
fmt.Printf("%v", data)
|
||||
}
|
||||
|
||||
|
||||
/// Exercise
|
||||
|
||||
// - 1 Load people from json , load embedded objects
|
||||
// - 2 convert date_added in bookmarks to same as mozilla bookmarks (create a go function) // MSDN FILETIME
|
||||
|
||||
//https://stackoverflow.com/questions/19074423/how-to-parse-the-date-added-field-in-chrome-bookmarks-file
|
||||
//https://cs.chromium.org/chromium/src/base/time/time_win.cc?sq=package:chromium&type=cs
|
||||
//https://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux
|
||||
|
||||
|
||||
|
19
test2.json
Normal file
19
test2.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "this a website",
|
||||
"url": "htjtp://lol.com",
|
||||
"categories": ["tech", "news"],
|
||||
"people": [
|
||||
{
|
||||
"name": "toto",
|
||||
"age": 42
|
||||
},
|
||||
{
|
||||
"name": "hello",
|
||||
"age": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user