My program in Golang prints the first input in a file twice

王林
Release: 2024-02-10 10:03:09
forward
583 people have browsed it

我在 Golang 中的程序在文件中打印第一个输入两次

php Editor Xigua encountered an interesting problem when writing a program in Golang: how to print the first input twice in the file. This question seems simple, but it actually involves multiple aspects of knowledge such as how to read input, process strings, and file operations. Through in-depth research and practice, I successfully solved this problem and shared the solution with everyone. Next, I will introduce in detail the steps of my program implementation in Golang.

Question content

I try to get some csv formatted string as input and then print it to an actual csv file. It works, but it prints the first string twice.

My code looks like this:

func main() {
    scanner := bufio.newscanner(os.stdin)
    n := 0
    inputfile, err := os.create("input.csv") //create the input.csv file
    if err != nil {
        log.fatal(err)
    }

    csvwriter := csv.newwriter(inputfile)

    fmt.println("how many records ?")
    fmt.scanln(&n)
    fmt.println("enter the records")
    var lines [][]string
    for i := 0; i < n; i++ {
        scanner.scan()
        text := scanner.text()
        lines = append(lines, []string{text})
        err := csvwriter.writeall(lines)
        if err != nil {
            return
        }
    }
    csvwriter.flush()
    inputfile.close()
}
Copy after login

For n=2 and records:

abcd, efgh, ijklmn
opq, rstu, vwxyz
Copy after login

The output looks like this:

"abcd, efgh, ijklmn"
"abcd, efgh, ijklmn"
"opq, rstu, vwxyz"
Copy after login

This is my first time using golang and I'm a bit lost :d

Solution

You are writing the csv in a loop so that the first line prints double. Here is the corrected code.

package main

import (
    "bufio"
    "encoding/csv"
    "fmt"
    "log"
    "os"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    n := 0
    inputFile, err := os.Create("input.csv") //create the input.csv file
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        inputFile.Close()
    }()

    csvwriter := csv.NewWriter(inputFile)
    defer func() {
        csvwriter.Flush()
    }()
    fmt.Println("How many records ?")
    fmt.Scanln(&n)
    fmt.Println("Enter the records")
    var lines [][]string
    for i := 0; i < n; i++ {
        scanner.Scan()
        text := scanner.Text()
        lines = append(lines, []string{text})

    }
    err = csvwriter.WriteAll(lines)
    if err != nil {
        return
    }
}
Copy after login

The above is the detailed content of My program in Golang prints the first input in a file twice. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!