用go语言读写通达信的日线数据

wangyanlb · · 6574 次点击
package main import ( "bytes" "encoding/binary" // "encoding/gob" "fmt" "io" // "log" "os" ) type StockData struct { Date, Open, High, Low, Close, Amount, Vol, Reservation int } func main() { //buf, err := ioutil.ReadFile("D:\\新建文本文档.txt") windown下面的文件打开路径 //buf, err := ioutil.ReadFile("../src/hangqing/sh000001.day") f, err := os.OpenFile("../src/hangqing/sh000001.day", os.O_RDONLY, 0666) if err != nil { fmt.Println(err.Error()) return } buf := make([]byte, 32) // var str string
#2
更多评论
奇怪,代码格式有点混乱。有得代码没有框起来。
#1
package main import ( "bytes" "encoding/binary" "fmt" "io" "os" ) type StockFile struct { Date, Open, High, Low, Close int32 Amount float32 Vol int32 Preclose int32 } type StockData struct { Date int32 Open, High, Low, Close, Amount float32 Vol, Preclose int32 } func main() { //buf, err := ioutil.ReadFile("D:\\新建文本文档.txt") windown下面的文件打开路径 f, err := os.OpenFile("../src/hangqing/sh000001.day", os.O_RDONLY, 0666) if err != nil { fmt.Println(err.Error()) return } buf := make([]byte, 32) /*重置文件指针,否则读不到内容的。*/ f.Seek(0, os.SEEK_SET) for { n, ferr := f.Read(buf) if ferr != nil && ferr != io.EOF { fmt.Println(ferr.Error()) break } if n == 0 { break } //str += string(buf[0:n]) b_buf := bytes.NewBuffer(buf[0:n]) var x StockFile var y StockData binary.Read(b_buf, binary.LittleEndian, &x) y.Date = x.Date y.Open = float32(x.Open) / 100 y.High = float32(x.High) / 100 y.Low = float32(x.Low) / 100 y.Close = float32(x.Close) / 100 y.Preclose = x.Preclose y.Amount = x.Amount y.Vol = x.Vol fmt.Println(y) } f.Close() }
#3