0

I'm writing a small pragram to number the paragraph:

  1. put paragraph number in front of each paragraph in the form of [1]..., [2]....
  2. Article title should be excluded.

Here is my program:

package main

import (
    "fmt"
    "io/ioutil"
)

var s_end = [3]string{".", "!", "?"}

func main() {
    b, err := ioutil.ReadFile("i_have_a_dream.txt")
    if err != nil {
        panic(err)
    }

    p_num, s_num := 1, 1

    for _, char := range b {
        fmt.Printf("[%s]", p_num)
        p_num += 1
        if char == byte("\n") {
            fmt.Printf("\n[%s]", p_num)
            p_num += 1
        } else {
            fmt.Printf(char)
        }
    }
}

http://play.golang.org/p/f4S3vQbglY

I got this error:

prog.go:21: cannot convert "\n" to type byte
prog.go:21: cannot convert "\n" (type string) to type byte
prog.go:21: invalid operation: char == "\n" (mismatched types byte and string)
prog.go:25: cannot use char (type byte) as type string in argument to fmt.Printf
[process exited with non-zero status]

How to convert string to byte?

What is the general practice to process text? Read in, parse it by byte, or by line?

Update

I solved the problem by converting the buffer byte to string, replacing strings by regular expression. (Thanks to @Tomasz Kłak for the regexp help)

I put the code here for reference.

package main

import (
    "fmt"
    "io/ioutil"
    "regexp"
)


func main() {
    b, err := ioutil.ReadFile("i_have_a_dream.txt")
    if err != nil {
        panic(err)
    }

    s := string(b)
    r := regexp.MustCompile("(\r\n)+")
    counter := 1

    repl := func(match string) string {
        p_num := counter
        counter++
        return fmt.Sprintf("%s [%d] ", match, p_num)
    }

    fmt.Println(r.ReplaceAllStringFunc(s, repl))
}
1
  • 1
    Use single quotes '\n' instead of double quotes "\n" to denote a byte. Also your Printf should have a formatting string. See `play.golang.org/p/4DIjm6-N32 Commented Jan 11, 2015 at 8:40

2 Answers 2

9

Using "\n" causes it to be treated as an array, use '\n' to treat it as a single char.

Sign up to request clarification or add additional context in comments.

Comments

2

A string cannot be converted into a byte in a meaningful way. Use one of the following approaches:

  • If you have a string literal like "a", consider using a rune literal like 'a' which can be converted into a byte.
  • If you want to take a byte out of a string, use an index expression like myString[42].
  • If you want to interpret the content of a string as a (decimal) number, use strconv.Atoi() or strconv.ParseInt().

Please notice that it is customary in Go to write programs that can deal with Unicode characters. Explaining how to do this would be too much for this answer, but there are tutorials out there which explain what kind of things to pay attention to.

2 Comments

Thank you for your explanation and remarks on Unicode consideration. I converted the byte to string using s:=string(b), and solved the problem by using regular expression replacement.
@Nick You don't need to convert to string; just use regexp.(*Regexp).ReplaceAllFunc() instead of regexp.(*Regexp).ReplaceAllStringFunc().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.