Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
ff9e657f
Commit
ff9e657f
authored
Jul 08, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WriteFile util function
R=rsc DELTA=41 (41 added, 0 deleted, 0 changed) OCL=31349 CL=31358
parent
764b6ec1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
+41
-0
utils.go
src/pkg/io/utils.go
+16
-0
utils_test.go
src/pkg/io/utils_test.go
+25
-0
No files found.
src/pkg/io/utils.go
View file @
ff9e657f
...
@@ -28,3 +28,19 @@ func ReadFile(filename string) ([]byte, os.Error) {
...
@@ -28,3 +28,19 @@ func ReadFile(filename string) ([]byte, os.Error) {
defer
f
.
Close
();
defer
f
.
Close
();
return
ReadAll
(
f
);
return
ReadAll
(
f
);
}
}
// WriteFile writes data to a file named by filename.
// If the file does not exist, WriteFile creates it with permissions perm.
//
func
WriteFile
(
filename
string
,
data
[]
byte
,
perm
int
)
os
.
Error
{
f
,
err
:=
os
.
Open
(
filename
,
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
,
perm
);
if
err
!=
nil
{
return
err
;
}
n
,
err
:=
f
.
Write
(
data
);
if
err
==
nil
&&
n
<
len
(
data
)
{
err
=
ErrShortWrite
;
}
f
.
Close
();
return
err
;
}
src/pkg/io/utils_test.go
View file @
ff9e657f
...
@@ -7,6 +7,7 @@ package io
...
@@ -7,6 +7,7 @@ package io
import
(
import
(
"io"
;
"io"
;
"os"
;
"os"
;
"strings"
;
"testing"
;
"testing"
;
)
)
...
@@ -35,3 +36,27 @@ func TestReadFile(t *testing.T) {
...
@@ -35,3 +36,27 @@ func TestReadFile(t *testing.T) {
checkSize
(
t
,
filename
,
uint64
(
len
(
contents
)));
checkSize
(
t
,
filename
,
uint64
(
len
(
contents
)));
}
}
func
TestWriteFile
(
t
*
testing
.
T
)
{
filename
:=
"_obj/rumpelstilzchen"
;
data
:=
"Programming today is a race between software engineers striving to "
"build bigger and better idiot-proof programs, and the Universe trying "
"to produce bigger and better idiots. So far, the Universe is winning."
;
if
err
:=
WriteFile
(
filename
,
strings
.
Bytes
(
data
),
0644
);
err
!=
nil
{
t
.
Fatalf
(
"WriteFile %s: %v"
,
filename
,
err
);
}
contents
,
err
:=
ReadFile
(
filename
);
if
err
!=
nil
{
t
.
Fatalf
(
"ReadFile %s: %v"
,
filename
,
err
);
}
if
string
(
contents
)
!=
data
{
t
.
Fatalf
(
"contents = %q
\n
expected = %q"
,
string
(
contents
),
data
);
}
// cleanup
os
.
Remove
(
filename
);
// ignore error
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment