Go Lang Scan doesent扫描下一行

时间:2016-04-01 12:38:34

标签: go scanning

此扫描仪正在扫描下一行。当你看到结果时,我会更详细地解释它......

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
)

func main() {
    var inputFileName string
    var write string

    fmt.Scanln(&inputFileName)

    //func Join(a []string, sep string) string
    s := []string{inputFileName, ".txt"}
    inputFileName = strings.Join(s, "")

    creator, err := os.Create(inputFileName)
    check(err)

    /*
     *Writing
     */

    fmt.Printf("The file name with %s what do you want to write?", inputFileName)
    fmt.Scanln(&write)
    if len(write) <= 0 {
        panic("Cant be empty")
    }

    byteStringWrite := []byte(write)
    //func (f *File) Write(b []byte) (n int, err error)
    fmt.Println("BYTE : ", byteStringWrite)
    fmt.Println("NONBYTE : ", write)
    _, errWriter := creator.Write(byteStringWrite)
    check(errWriter)

    /**
     *Reading File
     */

    read, errRead := ioutil.ReadFile(inputFileName)
    check(errRead)
    readString := string(read)
    fmt.Println("*******************FILE*********************")
    fmt.Println(readString)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

结果:

Sample.txt //My User Input
The file name with Sample.txt what do you want to write?Hello World
BYTE :  [72 101 108 108 111]
NONBYTE :  Hello
*******************FILE*********************
Hello

所以在这里你可以看到它寻找空间。空间后的含义会自动退出。有人能帮我解决这个问题吗?三江源。

修改

使用bufio.ReadString();

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
    "bufio"
)

func main() {
    var inputFileName string
    var write string

    bio := bufio.NewReader(os.Stdin)
    inputFileName, err := bio.ReadString('\n')
    fmt.Println(inputFileName)

    //func Join(a []string, sep string) string
    s := []string{inputFileName, ".txt"}
    inputFileName = strings.Join(s, "")

    creator, err := os.Create(inputFileName)
    check(err)

    /*
     *Writing
     */

    fmt.Printf("The file name with %s what do you want to write?", inputFileName)
    fmt.Scanln(&write)
    if len(write) <= 0 {
        panic("Cant be empty")
    }

    byteStringWrite := []byte(write)
    //func (f *File) Write(b []byte) (n int, err error)
    fmt.Println("BYTE : ", byteStringWrite)
    fmt.Println("NONBYTE : ", write)
    _, errWriter := creator.Write(byteStringWrite)
    check(errWriter)

    /**
     *Reading File
     */

    read, errRead := ioutil.ReadFile(inputFileName)
    check(errRead)
    readString := string(read)
    fmt.Println("*******************FILE*********************")
    fmt.Println(readString)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

结果:

amanuel2:~/workspace/pkg_os/07_Practice $ go run main.go 
Sample
The file name with Sample
.txt what do you want to write?Something Else
BYTE :  [83 111 109 101 116 104 105 110 103]
NONBYTE :  Something
*******************FILE*********************
Something

给我正确的.txt ..但与上面相同的问题,它需要空间

1 个答案:

答案 0 :(得分:2)

这正是fmt.Scanln应该做的事情:

  

扫描扫描从标准输入读取的文本,连续存储   空格分隔的值到连续的参数中。换行计为   空间。它返回成功扫描的项目数。如果那样的话   小于参数的数量,错误将报告原因。

如果您想阅读一行文字,请使用bufio.Reader

bio := bufio.NewReader(os.Stdin)

// in case you want a string which doesn't contain the newline
line, hasMoreInLine, err := bio.ReadLine()
s := string(line)    
fmt.Println(s)

// in case you need a string which contains the newline
s, err := bio.ReadString('\n')
fmt.Println(s)