Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:37:05+00:00 2026-06-11T12:37:05+00:00

I am trying to take input from the keyboard and then store it in

  • 0

I am trying to take input from the keyboard and then store it in a text file but I am a bit confused on how to actually do it.

My current code is as follow at the moment:

// reads the file txt.txt 
bs, err := ioutil.ReadFile("text.txt")
if err != nil {
      panic(err)
}

// Prints out content
textInFile := string(bs)
fmt.Println(textInFile)

// Standard input from keyboard
var userInput string
fmt.Scanln(&userInput)

//Now I want to write input back to file text.txt
//func WriteFile(filename string, data []byte, perm os.FileMode) error

inputData := make([]byte, len(userInput))

err := ioutil.WriteFile("text.txt", inputData, )

There are so many functions in the “os” and “io” packages. I am very confused about which one I actually should use for this purpose.

I am also confused about what the third argument in the WriteFile function should be. In the documentation is says of type ” perm os.FileMode” but since I am new to programming and Go I am a bit clueless.

Does anybody have any tips on how to proced?
Thanks in advance,
Marie

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:37:07+00:00Added an answer on June 11, 2026 at 12:37 pm
    // reads the file txt.txt 
    bs, err := ioutil.ReadFile("text.txt")
    if err != nil { //may want logic to create the file if it doesn't exist
          panic(err)
    }
    
    var userInput []string
    
    var err error = nil
    var n int
    //read in multiple lines from user input
    //until user enters the EOF char
    for ln := ""; err == nil; n, err = fmt.Scanln(ln) {
        if n > 0 {  //we actually read something into the string
            userInput = append(userInput, ln)
        } //if we didn't read anything, err is probably set
    }
    
    //open the file to append to it
    //0666 corresponds to unix perms rw-rw-rw-,
    //which means anyone can read or write it
    out, err := os.OpenFile("text.txt", os.O_APPEND, 0666)
    defer out.Close() //we'll close this file as we leave scope, no matter what
    
    if err != nil { //assuming the file didn't somehow break
        //write each of the user input lines followed by a newline
        for _, outLn := range userInput {
            io.WriteString(out, outLn+"\n")
        }
    }
    

    I’ve made sure this compiles and runs on play.golang.org, but I’m not at my dev machine, so I can’t verify that it’s interacting with Stdin and the file entirely correctly. This should get you started though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to make a code that will take input from a user
So I'm trying to take an input from a user using Scanner(System.in) but when
I've been trying to create some php code that would let me take input
I am basically trying to take input as RGB Stream from Kinect for Windows
I am trying to take input from the user and want to compile regex
I’m trying to make something that will take lines of input from the user,
I am trying to take a CSV file as input and transform it into
I am trying to get a decimal input from the keyboard, and it is
I'm trying to take a string and then retrieve the required elements from it.
I am trying to create an application that reads keyboard input from a HID.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.