go读取通达信本地数据

之前在网上找了段python版本的读取通达信本地数据的程序https://www.cnblogs.com/pu369/p/13099785.html

练习改成go版的。

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "io"
    "os"
)

type stlHead struct {
    A1 int32
    A2 int32
    A3 int32
    A4 int32
    A5 int32
    A6 float32
    A7 int32
    A8 int32
}

func main() {

    ReadFile("D:/通达信/vipdoc/sh/lday/sh000001.day")

}

func ReadFile(filePath string) {
    buf := bytes.NewBufferString("")
    file, err := os.Open(filePath)
    if err != nil {
        fmt.Print(err)
        return
    }
    head := new(stlHead)
    for { 
        if err := binary.Read(file, binary.LittleEndian, head); err != nil {
            fmt.Println(err)
            if err == io.EOF {
                fmt.Println(buf)
            }
            return
        }
        b1 := fmt.Sprintf("A1-A8: %d	%.2f	%.2f	%.2f	%.2f	%f	%d	%d	
",
            head.A1, float32(head.A2)/100.0, float32(head.A3)/100.0, float32(head.A4)/100.0, float32(head.A5)/100.0,
            head.A6, head.A7, head.A8)
        buf.WriteString(b1)
    }

}

参考:http://blog.sina.com.cn/s/blog_b8f7d1490102w04o.html

https://www.jianshu.com/p/b598ede612d8

https://www.jianshu.com/p/0c60f4a28553

https://www.sunzhongwei.com/golang-float-with-string-int-type-of-transformation-and-how-to-keep-a-small-digital?from=sidebar_new

https://hacpai.com/article/1564806399214

 大智慧新一代格式:https://blog.csdn.net/zjbo_123/article/details/6640785?locationNum=3&fps=1

原文地址:https://www.cnblogs.com/pu369/p/13132169.html