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
3f5966dc
Commit
3f5966dc
authored
Aug 02, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
io: consolidate multi_reader and multi_writer into a single file, multi.go
R=rsc CC=golang-dev
https://golang.org/cl/1860046
parent
55badd47
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
75 deletions
+56
-75
Makefile
src/pkg/io/Makefile
+1
-2
multi.go
src/pkg/io/multi.go
+24
-0
multi_test.go
src/pkg/io/multi_test.go
+31
-1
multi_writer.go
src/pkg/io/multi_writer.go
+0
-31
multi_writer_test.go
src/pkg/io/multi_writer_test.go
+0
-41
No files found.
src/pkg/io/Makefile
View file @
3f5966dc
...
...
@@ -7,8 +7,7 @@ include ../../Make.$(GOARCH)
TARG
=
io
GOFILES
=
\
io.go
\
multi_reader.go
\
multi_writer.go
\
multi.go
\
pipe.go
\
include
../../Make.pkg
src/pkg/io/multi
_reader
.go
→
src/pkg/io/multi.go
View file @
3f5966dc
...
...
@@ -34,3 +34,27 @@ func (mr *multiReader) Read(p []byte) (n int, err os.Error) {
func
MultiReader
(
readers
...
Reader
)
Reader
{
return
&
multiReader
{
readers
}
}
type
multiWriter
struct
{
writers
[]
Writer
}
func
(
t
*
multiWriter
)
Write
(
p
[]
byte
)
(
n
int
,
err
os
.
Error
)
{
for
_
,
w
:=
range
t
.
writers
{
n
,
err
=
w
.
Write
(
p
)
if
err
!=
nil
{
return
}
if
n
!=
len
(
p
)
{
err
=
ErrShortWrite
return
}
}
return
len
(
p
),
nil
}
// MultiWriter creates a writer that duplicates its writes to all the
// provided writers, similar to the Unix tee(1) command.
func
MultiWriter
(
writers
...
Writer
)
Writer
{
return
&
multiWriter
{
writers
}
}
src/pkg/io/multi_
reader_
test.go
→
src/pkg/io/multi_test.go
View file @
3f5966dc
// Copyright 2010 The Go Authors. All rights reserved.
// Copyright 2010 The Go Authors.
All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
...
...
@@ -6,6 +6,9 @@ package io_test
import
(
.
"io"
"bytes"
"crypto/sha1"
"fmt"
"os"
"strings"
"testing"
...
...
@@ -56,3 +59,30 @@ func TestMultiReader(t *testing.T) {
expectRead
(
5
,
"foo "
,
nil
)
})
}
func
TestMultiWriter
(
t
*
testing
.
T
)
{
sha1
:=
sha1
.
New
()
sink
:=
new
(
bytes
.
Buffer
)
mw
:=
MultiWriter
(
sha1
,
sink
)
sourceString
:=
"My input text."
source
:=
strings
.
NewReader
(
sourceString
)
written
,
err
:=
Copy
(
mw
,
source
)
if
written
!=
int64
(
len
(
sourceString
))
{
t
.
Errorf
(
"short write of %d, not %d"
,
written
,
len
(
sourceString
))
}
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
sha1hex
:=
fmt
.
Sprintf
(
"%x"
,
sha1
.
Sum
())
if
sha1hex
!=
"01cb303fa8c30a64123067c5aa6284ba7ec2d31b"
{
t
.
Error
(
"incorrect sha1 value"
)
}
if
sink
.
String
()
!=
sourceString
{
t
.
Error
(
"expected %q; got %q"
,
sourceString
,
sink
.
String
())
}
}
src/pkg/io/multi_writer.go
deleted
100644 → 0
View file @
55badd47
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
io
import
"os"
type
multiWriter
struct
{
writers
[]
Writer
}
func
(
t
*
multiWriter
)
Write
(
p
[]
byte
)
(
n
int
,
err
os
.
Error
)
{
for
_
,
w
:=
range
t
.
writers
{
n
,
err
=
w
.
Write
(
p
)
if
err
!=
nil
{
return
}
if
n
!=
len
(
p
)
{
err
=
ErrShortWrite
return
}
}
return
len
(
p
),
nil
}
// MultiWriter creates a writer that duplicates its writes to all the
// provided writers, similar to the Unix tee(1) command.
func
MultiWriter
(
writers
...
Writer
)
Writer
{
return
&
multiWriter
{
writers
}
}
src/pkg/io/multi_writer_test.go
deleted
100644 → 0
View file @
55badd47
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
io_test
import
(
.
"io"
"bytes"
"crypto/sha1"
"fmt"
"strings"
"testing"
)
func
TestMultiWriter
(
t
*
testing
.
T
)
{
sha1
:=
sha1
.
New
()
sink
:=
new
(
bytes
.
Buffer
)
mw
:=
MultiWriter
(
sha1
,
sink
)
sourceString
:=
"My input text."
source
:=
strings
.
NewReader
(
sourceString
)
written
,
err
:=
Copy
(
mw
,
source
)
if
written
!=
int64
(
len
(
sourceString
))
{
t
.
Errorf
(
"short write of %d, not %d"
,
written
,
len
(
sourceString
))
}
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
sha1hex
:=
fmt
.
Sprintf
(
"%x"
,
sha1
.
Sum
())
if
sha1hex
!=
"01cb303fa8c30a64123067c5aa6284ba7ec2d31b"
{
t
.
Error
(
"incorrect sha1 value"
)
}
if
sink
.
String
()
!=
sourceString
{
t
.
Error
(
"expected %q; got %q"
,
sourceString
,
sink
.
String
())
}
}
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