How do I save an entire string as a txt file in Go? -


i'm creating simple word processing program in go. command line, have 2 prompts:

$enter title:

$enter body:

the program supposed save document txt file , print command line. program works if user user types in one-word title , one-word body. if user types in several-word title, happen:

$enter title: here title  $enter body: s  $  title  -bash: title: command not found 

here code have far:

package main  import (     "fmt"     "io/ioutil" )  //create struct document type document struct {     title string     body []byte }  //save document txt file func (p *document) save() error {     filename := p.title + ".txt"     return ioutil.writefile(filename, p.body, 0600) }  //load document func loadpage(title string) (*document, error) {     filename := title + ".txt"     body, err := ioutil.readfile(filename)     if err != nil {         return nil, err     }     return &document{title: title, body: body}, nil }  //input document title , body.  func main() {     fmt.print("enter title: ")     var title string     fmt.scanln(&title)      fmt.print("enter body: ")     var body []byte     fmt.scanln(&body)  //save document , display on command line     p1 := &document{title: title, body: []byte(body)}     p1.save()     p2, _ := loadpage(title)     fmt.println(string(p2.body)) } 

what using bufio.readstring instead of fmt.scanln? not 100% how scanln works, pretty sure issue comes misuse of function. example bufio:

package main  import (         "bufio"         "fmt"         "io/ioutil"         "log"         "os"         "strings" )  // document represent document's data. type document struct {         title string         body  []byte }  // save dumps document txt file on disc. func (p *document) save() error {         filename := p.title + ".txt"         return ioutil.writefile(filename, p.body, 0600) }  // loadpage loads document disc. func loadpage(title string) (*document, error) {         filename := title + ".txt"         body, err := ioutil.readfile(filename)         if err != nil {                 return nil, err         }         return &document{title: title, body: body}, nil }  // input document title , body. func main() {         reader := bufio.newreader(os.stdin)         fmt.print("enter title: ")         title, err := reader.readstring('\n')         if err != nil {                 log.fatal(err)         }         title = strings.trimspace(title)          fmt.print("enter body: ")         body, err := reader.readstring('\n')         if err != nil {                 log.fatal(err)         }         body = strings.trimspace(body)          //save document , display on command line         p1 := &document{title: title, body: []byte(body)}         if err := p1.save(); err != nil {                 log.fatal(err)         }         p2, err := loadpage(title)         if err != nil {                 log.fatal(err)         }         fmt.println(string(p2.body)) } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -