SHARE
TWEET

Untitled

a guest Jul 20th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "io"
  7.     "os"
  8. )
  9.  
  10. var filename string
  11. var content string
  12. var nErr int
  13. var len int
  14.  
  15. func FileName() string {
  16.     fmt.Print("\nEnter a name for your new file: ")
  17.     fmt.Scan(&filename)
  18.     filename = filename + ".txt"
  19.     return filename
  20. }
  21.  
  22. func Content() string {
  23.     fmt.Printf("\nPlease type in the content you want to add to file '%s' : ", filename)
  24.     tmp := bufio.NewReader(os.Stdin)
  25.     content, _ = tmp.ReadString('\n')
  26.     //fmt.Scan(&content) ----------- THAT WORKS FINE
  27.     //content = strings.TrimRight(content, "\n")
  28.     return content
  29. }
  30.  
  31. func main() {
  32.     filename = FileName()
  33.     file, err := os.Create(filename)
  34.     defer file.Close()
  35.  
  36.     nErr = 1
  37.     checkError(err)
  38.  
  39.     content := Content()
  40.     len, err = io.WriteString(file, content)
  41.     nErr = 2
  42.     checkError(err)
  43. }
  44.  
  45. func checkError(err error) {
  46.     if nErr == 1 {
  47.         if err == nil {
  48.             fmt.Printf("\nFile '%s' successfully created!\n", filename)
  49.         } else {
  50.             fmt.Printf("\nCould not create file '%s'.\n", filename)
  51.         }
  52.     } else if nErr == 2 {
  53.         if err == nil {
  54.             fmt.Printf("\nContent successfully written %d characters to file '%s'!\n", len, filename)
  55.         } else {
  56.             fmt.Printf("\nCould not write content to file '%s'.\n", filename)
  57.         }
  58.     } else {
  59.         fmt.Print("\nSomething went wrong.\n")
  60.     }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top