Home > Back-end >  Undefined Golang functions, can someone check this over?
Undefined Golang functions, can someone check this over?

Time:12-31

The code below is intended to gather input from the user and output that to a file go-log.txt.

I receive the following errors:

Undefined- line 22 "os.Run"

Undefined- line 26:22 "textAdded"

Undefined- line 29:5 "os.Close"

package main

import (
    "fmt"
    "os"
)

func main() {

  YNQ := "yes"

  for YNQ == "yes" {

    fmt.Println("What do you want to add to the log?")

    textAdded = fmt.Scanln()


    openfile, err := os.Run("go-log.txt")
      if err != nil {
        panic(err)
      }
      openfile.Write(textAdded)

    //add date and time here


    fmt.Println("Would you like to add anything else?")

    fmt.Scanln(&YNQ)
  }
  openfile.Close()
}

CodePudding user response:

The os package has no methods such as Run or Write, which is why you see those errors for those functions.

Instead of Run and Write, perhaps you want WriteFile which will create the file if it does not exist. Or, you could use Open in place of Run

The other error pertains to your variable. To initialise a new variable in Go with a value, you need a colon

textAdded := fmt.Scanln()
  •  Tags:  
  • go
  • Related