Go language import file (fixed length parsing) (ii) increased read JSON configuration file

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Filename.json

===============

{
"FileName": "NAV20130104",
"Path": "F:\\golang\\test\\src\\photoweb"
}


NAV20130104 (only two lines below, showing 4 rows)

================
012013111502 General Fund 03000111112500896637721111253255355718000000000222987522222222222222223333333330044444442222222222222222
022013111511 General Fund (11000100017322428727400000157019839806000000000011032000000000000000000000000000000000000000000000000000



Note that make (map[string]interface{}, Len (b)) uses an empty interface that can hold any type

Package Main
Import (
"Bufio"
"Encoding/json"
"FMT"
"IO"
"Io/ioutil"
"Math"
"OS"
"StrConv"
)
Type rule struct {
ItemName string//Field name
DataType string//Type
length int//Lengths
tail int//decimal number
Chinesename string//Chinese name
}
var fdmap = map[string]string{}//{} is initialized to null
Func readFile (filename string) (map[string]string, error) {
Bytes, err: = Ioutil. ReadFile (filename)
If err! = Nil {
Fmt. Println ("ReadFile:", err.) Error ())
return nil, err
}
If err: = json. Unmarshal (bytes, &fdmap); Err! = Nil {
Fmt. Println ("Unmarshal:", err.) Error ())
return nil, err
}
Return Fdmap, Nil
}
Func Main () {

B: = []rule{
{"L_serialno", "C", 2, 0, "serial number"},
{"C_date", "C", 8, 0, "date"},
{"C_fundname", "C", 12, 0, "fund name"},
{"C_fundcode", "C", 6, 0, "Fund Code"},
{"F_totalnetvalue", "F", 16, 2, "Total Equity of the Fund"},
{"F_totalshares", "F", 16, 2, "Total fund share"},
{"F_netvalue", "F", 16, 4, "Net Fund unit"},
{"F_income", "F", 16, 2, "fund proceeds"},
{"F_wfincome", "F", 9, 4, "Million fund proceeds"},
{"F_yearincome", "F", 9, 4, "annual rate of Return"},
{"F_salefare", "F", 16, 2, "Sales service Charge"},

}

Reading configuration Files

Fdmap, err: = ReadFile ("./etc/filename.json")
If err! = Nil {
Fmt. Println ("ReadFile:", err.) Error ())
Return
}

Get file name

Path, OK: = fdmap["path"]
If!ok {
Fmt. Println ("Parse failed! ")
Return
}

filename, OK: = fdmap["filename"]
If!ok {
Fmt. Println ("Parse failed! ")
Return
}

Item: = Make (map[string]interface{}, Len (b))
F, err: = OS. Open (path + "\ \" + filename)//opening file

Open File Error handling
Defer F.close ()
If nil = = Err {
Buff: = Bufio. Newreader (f)//read-in cache
for {
Line, err: = Buff. ReadString (' \ n ')//read a line with ' \ n ' for Terminator
If err! = Nil | | Io. EOF = = Err {
Break
}
For I, Index: = 0, 0; I < Len (b); i++ {
if B[i].datatype = = "F" {
TMP, ERR: = StrConv. Parsefloat (Line[index:index+b[i].length], 10)
If err! = Nil {
Fmt. Println ("Conversion failed")
Return
}
Item[b[i].itemname] = Tmp/math. Pow (Float64 (B[i].tail))
} else {
Item[b[i].itemname] = Line[index:index+b[i].length]
}
Index + = B[i].length
}
Fmt. Println (item["C_fundcode"])
Fmt. Printf ("%16.2f\n", item["F_totalnetvalue"])
Insert the value you have taken into the database
}
}
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.