mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 14:26:04 +03:00
Practice sync map and xml
Signed-off-by: Bruce <weichou1229@gmail.com>
This commit is contained in:
parent
b97231eecb
commit
874d7b4987
10
playground/panic/trypanic.go
Normal file
10
playground/panic/trypanic.go
Normal file
@ -0,0 +1,10 @@
|
||||
package trypanic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Try() {
|
||||
var test *string
|
||||
fmt.Println(*test)
|
||||
}
|
14
playground/parse/parseInt_test.go
Normal file
14
playground/parse/parseInt_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseInt(t *testing.T) {
|
||||
var _, err = strconv.ParseInt("127", 0, 8)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
@ -18,3 +18,9 @@ func TestBasic(t *testing.T) {
|
||||
fmt.Println(*address2)
|
||||
|
||||
}
|
||||
|
||||
func TestPointer(t *testing.T) {
|
||||
var test *string = new(string)
|
||||
*test = "123"
|
||||
fmt.Println(test)
|
||||
}
|
||||
|
52
playground/syncmap/syncmap_test.go
Normal file
52
playground/syncmap/syncmap_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package syncmap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
var waitGroup sync.WaitGroup
|
||||
waitGroup.Add(10)
|
||||
var cache sync.Map
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
cache.Store("test", &Order{})
|
||||
order, _ := cache.Load("test")
|
||||
o := order.(*Order)
|
||||
fmt.Println(o)
|
||||
waitGroup.Done()
|
||||
}()
|
||||
}
|
||||
waitGroup.Wait()
|
||||
}
|
||||
|
||||
func TestSync2(t *testing.T) {
|
||||
var cache sync.Map
|
||||
|
||||
cache.Store("test", nil)
|
||||
|
||||
if order, ok := cache.Load("test"); ok && order != nil {
|
||||
fmt.Println(order)
|
||||
} else {
|
||||
fmt.Println("not exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSync3(t *testing.T) {
|
||||
var cache sync.Map
|
||||
orders := make([]Order, 2)
|
||||
|
||||
cache.Store("test2", orders[0])
|
||||
cache.Store("test", orders[0])
|
||||
|
||||
order, ok := cache.Load("test")
|
||||
fmt.Println(ok, order)
|
||||
|
||||
}
|
4089
playground/xml/2018Q1.xml
Executable file
4089
playground/xml/2018Q1.xml
Executable file
File diff suppressed because it is too large
Load Diff
63
playground/xml/xml_test.go
Normal file
63
playground/xml/xml_test.go
Normal file
@ -0,0 +1,63 @@
|
||||
package xml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
xj "github.com/basgys/goxml2json"
|
||||
"github.com/buger/jsonparser"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string `xml:"name"`
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
xmlFile, err := os.Open("2018Q1.xml")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer xmlFile.Close()
|
||||
json, err := xj.Convert(xmlFile)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(json.String())
|
||||
|
||||
//err = jsonparser.ObjectEach(json.Bytes(), objectEach,"xbrl")
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
//}
|
||||
|
||||
// 營業收入
|
||||
var revenue string
|
||||
_, err = jsonparser.ArrayEach(json.Bytes(), func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
||||
revenue, err = jsonparser.GetString(value, "#content")
|
||||
fmt.Println(revenue)
|
||||
}, "xbrl", "Revenue")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func parse(b []byte) {
|
||||
err := jsonparser.ObjectEach(b, objectEach, "xbrl")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func objectEach(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
|
||||
fmt.Printf("[Key]: [%s]\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
|
||||
return nil
|
||||
}
|
||||
|
||||
func arrayEach(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
||||
fmt.Printf("Array Value: '%s'\n Type: %s\n", string(value), dataType)
|
||||
}
|
Loading…
Reference in New Issue
Block a user