Golang reading file information into MongoDB

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

These days, in the project to do a handy function, read the phone attribution information, and then insert MongoDB

Cell Phone Attribution format

Number Provinces City Card type Code
130001 Jiangsu Changzhou Jiangsu Unicom 0519

The code is as follows

Package Mainimport ("Encoding/csv" "Encoding/json" "FMT" "io" "Labix.org/v2/mgo" "OS" "Runtime")/* Initialize configuration */func init () { File, _: = OS. Open ("Config.json") buf: = Make ([]byte, 2048) n, _: = file. Read (BUF) fmt. Println (String (BUF)) Err: = json. Unmarshal (Buf[:n], &config) if err! = Nil {panic (err) fmt. PRINTLN (Err)}}func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) importphoneinfo ()}var config config = config{}var worker = runtime. NUMCPU ()//mobile number type Phonearea struct {phone string ' phonestart ' area string ' province ' city string ' city ' phonet ype string "Phonetype" code string "code"}//Configuration type config struct {mongodbserver stringphoneareafile string}/* Import phone Geographic information * /func Importphoneinfo () {var chanphonearea = make (chan phonearea)//Mark complete Dones: = Make (chan struct{}, worker)//Read file information go add Phoneinfo (Chanphonearea)//Insert mongodbfor I: = 0; I < worker; i++ {Go dophoneinfo (Chanphonearea, Dones)}//waits to complete awaitforcloseresult (dones) fmt. Println ("Insert Complete")}/* get mobile Geographic Information */func Addphoneinfo (Chanphonearea chan<-PhoneaREA) {file, err: = OS. Open (config. Phoneareafile) If err! = Nil {fmt. Println ("Open file Failed", err) Return}defer file. Close () fmt. PRINTLN ("read phone geographic information file") Reader: = csv. Newreader (file) for {line, err: = Reader. Read () If err = = Io. EOF {Close (Chanphonearea) fmt. PRINTLN ("Mobile geo-Information file read complete") break} else if err! = Nil {close (Chanphonearea) fmt. Println ("Error:", err) Break}phonearea: = Phonearea{line[0], line[1], line[2], line[3], Line[4]}chanphonearea <- phonearea}}/* inserting information into Mongodb*/func dophoneinfo (Chanphonearea <-chan phonearea, Dones chan<-struct{}) {// Turn on loop session, Err: = MgO. Dial (config. Mongodbserver) If err! = Nil {fmt. PRINTLN ("error") Panic (ERR) Return}defer session. Close () C: = Session. DB ("Test"). C ("Phoneareainfo") for Phonearea: = Range Chanphonearea {fmt. Println ("Insert:", Phonearea) C.insert (&phonearea)}dones <-struct{}{}}func awaitforcloseresult (dones <-chan struct{}) {for {<-donesworker--if worker <= 0 {return}}}

  

Configuration file Config.json

{"Mongodbserver": "127.0.0.1", "Phoneareafile": "Phonearea.txt"}

  

Mobile Geographic Information phonearea.txt

Number, province, city, card type, area code 1300000, Beijing, Beijing, Beijing unicom GSM card, 0101300001, Jiangsu, Changzhou, Jiangsu Unicom Ruyi Tong Card, 05191300006, Jiangsu, Nanjing, Jiangsu unicom GSM card, 0251300010, Beijing, Beijing, Beijing Unicom GSM Card, 0101300011, Beijing, Beijing, Beijing unicom GSM card, 0101300012, Tianjin, Tianjin, Tianjin unicom GSM card, 0221300013, Tianjin, Tianjin, Tianjin unicom GSM card, 0221300014, Tianjin, Tianjin, Tianjin unicom GSM Card, 0221300015, Shandong, Zibo, Shandong Unicom GSM card, 05331300016, Shandong, Yantai, Shandong Unicom GSM card, 05351300017, Shandong, Jinan, Shandong Unicom GSM card, 05311300018, Tianjin, Tianjin, Tianjin unicom GSM Card, 0221300019, Tianjin, Tianjin, Tianjin unicom GSM card, 0221300020, Shanghai, Shanghai, Shanghai Unicom GSM card, 0211300021, Shanghai, Shanghai, Shanghai Unicom GSM card, 0211300022, Shanghai, Shanghai, Shanghai Unicom GSM Card, 0211300023, Shanghai, Shanghai, Shanghai Unicom GSM card, 0211300024, Shanghai, Shanghai, Shanghai Unicom GSM card, 0211300025, Jiangsu, Nanjing, Jiangsu unicom GSM card, 0251300026, Jiangsu, Nanjing, Jiangsu Unicom GSM Card, 025

  

Related Article

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.