Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "io"
- "os"
- )
- var filename string
- var content string
- var nErr int
- var len int
- func FileName() string {
- fmt.Print("\nEnter a name for your new file: ")
- fmt.Scan(&filename)
- filename = filename + ".txt"
- return filename
- }
- func Content() string {
- fmt.Printf("\nPlease type in the content you want to add to file '%s' : ", filename)
- tmp := bufio.NewReader(os.Stdin)
- content, _ = tmp.ReadString('\n')
- //fmt.Scan(&content) ----------- THAT WORKS FINE
- //content = strings.TrimRight(content, "\n")
- return content
- }
- func main() {
- filename = FileName()
- file, err := os.Create(filename)
- defer file.Close()
- nErr = 1
- checkError(err)
- content := Content()
- len, err = io.WriteString(file, content)
- nErr = 2
- checkError(err)
- }
- func checkError(err error) {
- if nErr == 1 {
- if err == nil {
- fmt.Printf("\nFile '%s' successfully created!\n", filename)
- } else {
- fmt.Printf("\nCould not create file '%s'.\n", filename)
- }
- } else if nErr == 2 {
- if err == nil {
- fmt.Printf("\nContent successfully written %d characters to file '%s'!\n", len, filename)
- } else {
- fmt.Printf("\nCould not write content to file '%s'.\n", filename)
- }
- } else {
- fmt.Print("\nSomething went wrong.\n")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement