Practice sync map and xml

Signed-off-by: Bruce <weichou1229@gmail.com>
This commit is contained in:
Bruce 2019-03-01 22:27:43 +08:00
parent b97231eecb
commit 874d7b4987
No known key found for this signature in database
GPG Key ID: C715526B381CAF28
6 changed files with 4234 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package trypanic
import (
"fmt"
)
func Try() {
var test *string
fmt.Println(*test)
}

View 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)
}
}

View File

@ -18,3 +18,9 @@ func TestBasic(t *testing.T) {
fmt.Println(*address2) fmt.Println(*address2)
} }
func TestPointer(t *testing.T) {
var test *string = new(string)
*test = "123"
fmt.Println(test)
}

View 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

File diff suppressed because it is too large Load Diff

View 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)
}