Golang Preliminary interview: The Pit Master's

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

With Golang and Perl script comparison, first think at least one times ... The result is not the general pit father, is simply the Hang Ye.

Perl script

#!/bin/bash

Source/etc/profile;

function Extractandzip () {
_debug "$FUNCNAME, $@";
Local logfile= "${2}"
Local gzipfile= "${1}"
Perl-ne ' if (m/([^]*) \-([^]*) \[([^]*) [\+\-][0-9]{4}\] \ "(\-| ( ([^ ]*) )? ([^\?\;\% ]*) ([\?\;\%]([^ ]*))? ( ([^\"]*))?) \" ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([^ ]*) (\-| ([^\-]+)) /) {printf ("%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\n", ${1}, ${2}, ${3}, ${6}, ${7}, ${9}, ${11}, ${12}, ${13}, ${14}, ${15}, ${16}, ${17}*1000, ${19}*1000)} ' ${logfile} | gzip > ${gzipfile};
}

Extractandzip "$@"

www-data@dc26:/data2/rsynclog/gotest$ Time bash perl.sh result.gz 2014-06-17+yyexplorer+58.215.138.18+ Yyexplorer-access.log
/data/sa/profile_common:line 23:ulimit:open Files:cannot Modify limit:operation not permitted
Perl.sh:line 6: _debug:command not found

Real 4m5.222s
User 5m54.630s
SYS 0m9.720s

6 minutes all done ...

Golang Code:

Package Main

Import (
"Bufio"
"Compress/gzip"
"FMT"
"OS"
"RegExp"
"StrConv"
"Strings"
)

var recordregexp = RegExp. Mustcompile (' ([^]*) \-([^]*) \[([^]*) [\+\-][0-9]{4}\] \ "(\-| ( ([^ ]*) )? ([^\?\;\% ]*) ([\?\;\%]([^ ]*))? ( ([^\"]*))?) \" ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([^ ]*) (\-| ([^\-]+)) `)

Func toint (str string) int {
Val, err: = StrConv. Atoi (str)
If err! = Nil {
Return Val
}
return 0
}

Func Main () {
If Len (OS. Args) < 3 {
Fmt. Println ("Usage:", OS.) Args[0], "<out_zip_file>", "<in_txt_file1...>")
Os. Exit (1)
}

Outzipfile, err: = OS. Create (OS. ARGS[1])
If err! = Nil {
Fmt. Errorf ("Error:%s\n", err. Error ())
Os. Exit (1)
}
Defer Outzipfile.close ()

Intxtfiles: = Make ([]*os. File, Len (OS. Args)-2)
For _, Path: = Range OS. Args[2:] {
File, err: = OS. Open (PATH)
If err! = Nil {
Fmt. Errorf ("Error:%s\n", err. Error ())
Os. Exit (1)
}
Defer file. Close ()
Intxtfiles = Append (intxtfiles, file)
}

Zipio: = gzip. Newwriter (Outzipfile)
Defer Zipio.close ()
Out: = Bufio. Newwriter (Zipio)
For _, File: = Range Intxtfiles {
Scan: = Bufio. Newscanner (file)
For scan. Scan () {
Line: = Scan. Bytes ()
Items: = Recordregexp.findsubmatch (line)
Out. Write (Items[1])
Out. Write ([]byte ("\ T"))
Out. Write (Items[2])
Out. Write ([]byte ("\ T"))
Out. Write (Items[3])
Out. Write ([]byte ("\ T"))
Out. Write (Items[6])
Out. Write ([]byte ("\ T"))
Out. Write (Items[7])
Out. Write ([]byte ("\ T"))
Out. Write (Items[9])
Out. Write ([]byte ("\ T"))
Out. Write (items[11])
Out. Write ([]byte ("\ T"))
Out. Write (items[12])
Out. Write ([]byte ("\ T"))
Out. Write (items[13])
Out. Write ([]byte ("\ T"))
Out. Write (items[14])
Out. Write ([]byte ("\ T"))
Out. Write (items[15])
Out. Write ([]byte ("\ T"))
Out. Write (items[16])
Out. Write ([]byte ("\ T"))
Out. Write ([]byte (StrConv. Itoa (ToInt (String (items[17)) * 1000)))
Out. Write ([]byte ("\ T"))
Out. Write ([]byte (StrConv. Itoa (ToInt (String (items[19)) * 1000)))
Out. Write ([]byte ("\ n"))
}
Out. Flush ()
}

}
Results when manually kill:

16m only completed 3 of the 1 or so ... Pit your Grandpa ...

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.