File.WriteString()
はstring
引数を受け取りますが、の構造体へのポインタであるcur
を渡そうとします。これは明らかにコンパイル時エラーです。
user.User
は、次のように定義された構造体である:
type User struct {
// Uid is the user ID.
// On POSIX systems, this is a decimal number representing the uid.
// On Windows, this is a security identifier (SID) in a string format.
// On Plan 9, this is the contents of /dev/user.
Uid string
// Gid is the primary group ID.
// On POSIX systems, this is a decimal number representing the gid.
// On Windows, this is a SID in a string format.
// On Plan 9, this is the contents of /dev/user.
Gid string
// Username is the login name.
Username string
// Name is the user's real or display name.
// It might be blank.
// On POSIX systems, this is the first (or only) entry in the GECOS field
// list.
// On Windows, this is the user's display name.
// On Plan 9, this is the contents of /dev/user.
Name string
// HomeDir is the path to the user's home directory (if they have one).
HomeDir string
}
は、ファイルに出力したいものを選択して、最も可能性の高いUsername
フィールドまたは多分Name
フィールド。これらはstring
型のフィールドなので、これらはあなたが問題なく通過することができます
if _, err = f.WriteString(cur.Username); err != nil {
panic(err)
}
あなたは完全User
構造体を作成したい場合は、あなたがfmt
パッケージ、便利fmt.Fprint()
またはfmt.Fprintf()
機能を使用することがあります。
if _, err = fmt.Fprintf(f, "%+v", cur); err != nil {
panic(err)
}
curとは何ですか? 'struct'はどうですか?どのデータをファイルに保存しますか? – nicovank
@nicovank cur、err:= user.Current()を文字列として使用する必要があります。出力ファイルをファイル –
インポートパッケージ 'fmt'にインポートし、' f.WriteString(fmt.Sprintf( "%+ v \ n"、cur)) ' – mkopriva