Why is there is a required error err
variable for the fmt.Scanln
function?
I also need help understanding the purpose of the fmt.Writeln
parameters.
The purpose of this program is to get input data from the user by the command line and output it to a file go-log.go
package main
import ("fmt"
"os")
func main() {
YNQ := "yes"
for YNQ == "yes" {
fmt.Println("What do you want to add to the log?")
openfile, err := os.Open("go-log.txt")
addedText, err := fmt.Scanln()
os.WriteFile("go-log.txt", []byte(addedText), 0666)
//add date and time here
fmt.Println("Would you like to add anything else?")
fmt.Scanln(&YNQ)
}
openfile.Close()
}
- errors recieved:
-cannot convert addedText (type int) to type [byte]
-undefined: openfile
CodePudding user response:
First of all, let's make your code readable with the go fmt
command:
package main
import (
"fmt"
"os"
)
func main() {
YNQ := "yes"
for YNQ == "yes" {
fmt.Println("What do you want to add to the log?")
openfile, err := os.Open("go-log.txt")
addedText, err := fmt.Scanln()
os.WriteFile("go-log.txt", []byte(addedText), 0666)
//add date and time here
fmt.Println("Would you like to add anything else?")
fmt.Scanln(&YNQ)
}
openfile.Close()
}
Second, let's try to understand and answer your questions. You are asking:
Why is there is a required error
err
variable for thefmt.Scanln
function?
- Go compiler forces you to assign either all return values to variables or none of them. You can also use a placeholder
_
marking that you are not interested in this variable. Please, read this article for more information: https://gobyexample.com/multiple-return-values. - Scanln returns the error because there might be an error when you scan a standard output.
I also need help understanding the purpose of the
fmt.Writeln
parameters.
There is no function Writeln
in the package fmt
: https://pkg.go.dev/fmt#pkg-index
cannot convert addedText (type int) to type [byte]
Please, carefully read the documentation for the fmt.Scanln
function: https://pkg.go.dev/fmt#Scanln. It does not return text. It writes what it reads from the stdin
to its arguments. These arguments are supposed to be pointers to variables you want to populate: https://www.geeksforgeeks.org/fmt-scanln-function-in-golang-with-examples/
undefined: openfile
The openfile
variable is indeed not available in this scope. Please, read this article about scopes in Go: https://medium.com/golangspec/scopes-in-go-a6042bb4298c
The purpose of this program is to get input data from the user by the command line and output it to a file go-log.go
The program should look like this:
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
f, err := os.OpenFile("go-log.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
YNQ := "yes"
for YNQ == "yes" {
fmt.Println("What do you want to add to the log?")
scanner := bufio.NewScanner(os.Stdin)
var line string
if scanner.Scan() {
line = scanner.Text()
} else {
log.Fatalln("Cannot read from stdin.")
}
if _, err = f.WriteString(line "\n"); err != nil {
log.Fatalln(err)
}
fmt.Println("Would you like to add anything else?")
_, err := fmt.Scanln(&YNQ)
if err != nil {
log.Fatalln(err)
}
}
}
CodePudding user response:
See its documentation: Scanln
The doc refers to Scan
and this explains what the functions return ("the number of items") and why they would error:
"It returns the number of items successfully scanned. If that is less than the number"