Golang-Two ways to read a file one line of problems caused by

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

The first two days of fragmentary read the foundation of Golang, think of a small project to practice practiced hand, but there is a very difficult problem
What I'm going to do is site path blasting
So I'm going to read a line of lines from the text dictionary and stitch it up with the domain name, but I'm having trouble running the program.
Here is a small fragment

400 Bad Request-----http://www.xxx.com/channel.asp400 Bad Request-----http://www.xxx.com/index.asp404 Not Found-----http://www.xxx.com/admin.asp

There is no error in the program itself, but the result of the operation is rather strange.
Bad Request?
That's not the point I'm talking about, but the problem I'm finding is that all the addresses in front of you will show a bit of bad Request except for the last address.
After several rounds of testing, I think it should be a problem with the URL stitching.

My stitching function is like this

func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {    allURLTxt := bufio.NewReader(fURL)    for {        urlpath, readerError := allURLTxt.ReadString('\n')        newurl := baseurl + strings.Replace(urlpath, "\n", "", -1)        urlList = append(urlList, newurl)        if readerError == io.EOF {            fmt.Printf("\n读取字典完成,准备开始,请等待...\n")            return urlList        }    }}

I changed the way I took a line to Bufio. Newscanner is normal.

func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {    allURLTxt := bufio.NewScanner(fURL)    for allURLTxt.Scan() {        newurl := baseurl + allURLTxt.Text()        urlList = append(urlList, newurl)    }    fmt.Printf("\n读取字典完成,准备开始,请等待...\n")    return urlList}

Read a file online a lot of people write the article is the first method, but I do not know what the problem caused the occurrence of this situation
I went to check the API documentation.

Func Newreader (rd IO. Reader) *reader//newreader returns a new reader whose buffer has the default size. Func (b *reader) ReadString (Delim byte) (string, error)//readstring reads until the first occurrence of delim in the input , returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error Itse LF (often io. EOF). ReadString returns ERR! = Nil if and only if the returned data does not end in Delim. For simple uses, a Scanner is more convenient. Func Newscanner (R io. Reader) *scanner//newscanner returns a new Scanner to read from R. The Split function defaults to Scanlines.  Func (S *scanner) Scan () Bool//scan advances the Scanner to the next token, which'll then be available through the Bytes or Text method. It returns False when the scan is stops, either by reaching the end of the input or an error. After Scan returns false, the Err method would return any ERROr that occurred during scanning, except if it is IO. EOF, ERR would return nil. Scan panics if the Split function returns tokens without advancing the input. This is a common the error mode for scanners. Func (S *scanner) Text () String//text returns the most recent tokens generated by a call to Scan as a newly allocated strin G holding its bytes.

According to the above API documentation, the two differences are when they return a string, one is the data + delimiter, one is a row of data, without delimiters
Although my first method also uses strings. The Replace method replaces "\ n" with the "" "null character, but may still be a bit of a bizarre thing

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.