Created
March 6, 2016 08:44
-
-
Save akkijp/8e91b51921e82ba1121a to your computer and use it in GitHub Desktop.
golangでファイルへの保存と読み出しテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
// "os" | |
// "net/http" | |
// "time" | |
) | |
type Page struct { | |
Title string | |
Body []byte | |
} | |
func (p *Page) save() error { | |
fname := p.Title + ".txt" | |
return ioutil.WriteFile(fname, p.Body, 0600) | |
} | |
func loadPage(title string) (*Page, error) { | |
fname := title + ".txt" | |
body, err := ioutil.ReadFile(fname) | |
if err != nil { | |
panic(err) | |
return nil, err | |
} | |
return &Page{Title: title, Body: body}, nil | |
} | |
func main() { | |
p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")} | |
p1.save() | |
p2, _ := loadPage("testPage") | |
fmt.Println(string(p2.Body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment