I need to learn User and write to a text file, how can I convert to type string ?

Code:

 package main import "os" import "os/user" func main(){ f, err := os.OpenFile("test.txt", os.O_APPEND|os.O_WRONLY, 0600) if err != nil { panic(err) } defer f.Close() cur, err := user.Current() if err != nil { } else { if _, err = f.WriteString(cur); err != nil { panic(err) } } } 

Mistake:

 # command-line-arguments .\os.go:18:31: cannot use cur (type *user.User) as type string in argument to f.WriteString 

    2 answers 2

    You need to serialize the object. For example, in json. https://golang.org/src/encoding/json/example_test.go

      If you just need to write the user name, use the Username : field.

       if _, err = f.WriteString(cur.Username); err != nil { panic(err) } 

      If you need all the available information, see the answer colleagues and serialized user.