Feat : add writer example

This commit is contained in:
Antoine 2020-11-11 19:10:38 +01:00
parent 579ac0d2f3
commit 076238e196
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
4 changed files with 36 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
bin/
!bin/.gitkeep
./notes.txt

2
go.mod
View File

@ -2,6 +2,6 @@ module antoine-roux.ml/projects/go/test
go 1.15
require github.com/docker/docker-credential-helpers v0.6.3 // indirect
require github.com/docker/docker-credential-helpers v0.6.3
replace antoine-roux.ml/projects/go/test/employee => ./employee

1
notes.txt Normal file
View File

@ -0,0 +1 @@
hello world

33
writer.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"io"
"log"
"os"
)
func main() {
err := WriteString("hello world")
if err != nil {
log.Fatalln(err)
}
}
func WriteString(str string) (err error) {
var f *os.File
f, err = os.Create("./notes.txt")
if err != nil {
return
}
defer func() {
cerr := f.Close()
if err == nil {
err = cerr
}
}()
_, err = io.WriteString(f, str)
return
}