[go-nuts] type hash.Hash has no field Writer

101 views
Skip to first unread message

B-Ranger

unread,
May 7, 2010, 2:13:06 AM5/7/10
to golan...@googlegroups.com
Hy,

risking to give some folks a good reason for a facepalm I want to
present the following question:

Why is "hash.Hash" not having a "Writer"?

I tried.
> Sha1Hash := sha1.New()
> Sha1Hash.Writer.WriteString('SecretPasskey')
> print(Sha1Hash.Sum())

an received the error
> Sha1Hash.Writer undefined (type hash.Hash has no field Writer)

but the documentation says (http://golang.org/pkg/hash/):
> type Hash interface {
> ...
> io.Writer
> ...
> }

So I only see one possible explanation which is unsatisfying and
therefore I assume worng:
a) I can not access io.Writer because the "i" of "io" is lowercase or
b) hence I can not give any string to hash to my previous Sha1Hash-Func

I'd be glad if someone could help me out here,

Greetings,
B-Ranger

signature.asc

Jessta

unread,
May 7, 2010, 2:22:13 AM5/7/10
to B-Ranger, golan...@googlegroups.com
On Fri, May 7, 2010 at 4:13 PM, B-Ranger <B-Ra...@web.de> wrote:
> Hy,
>
> risking to give some folks a good reason for a facepalm I want to
> present the following question:
>
> Why is "hash.Hash" not having a "Writer"?

You should read about embedding and then read the rest of Effective Go.

http://golang.org/doc/effective_go.html#embedding

- jessta
--
=====================
http://jessta.id.au

jimmy frasche

unread,
May 7, 2010, 2:24:47 AM5/7/10
to Jessta, B-Ranger, golan...@googlegroups.com
http://golang.org/pkg/io/#Writer

so you want

Sha1Hash.Writer.Write([]byte("SecretPasskey"))

B-Ranger

unread,
May 7, 2010, 2:47:07 AM5/7/10
to golan...@googlegroups.com
So the solution is simple as I assumed but was not obvious to me,
just for anybody having the same problem:

> type Hash interface {
> ...
> io.Writer
> ...
> }

Does NOT say, that there is a field called "Writer" on which methods
working on "io.Writer" work BUT it says, that this type "Hash"
implements the methods that "io.Writer" implement. And this method(s)
are (http://golang.org/pkg/io/#Writer):
> type Writer interface {
> Write(p []byte) (n int, err os.Error)
> }

The solution of jimmy:
> Sha1Hash.Writer.Write([]byte("SecretPasskey"))
is also NOT working because, again it assumes the existence of an
embedded object. Instead the following works and is what I was looking for:
> Sha1Hash.Write([]byte("SecretPasskey"))

Thank you,
B-Ranger


signature.asc

peterGo

unread,
May 7, 2010, 3:43:19 AM5/7/10
to golang-nuts
B-Ranger,

package main

import (
"fmt"
"crypto/sha1"
)

func main() {
s := sha1.New()
s.Write([]byte("SecretPasskey"))
fmt.Println(s.Sum())
}

Peter
>  signature.asc
> < 1KViewDownload

peterGo

unread,
May 7, 2010, 3:49:16 AM5/7/10
to golang-nuts
B-Ranger,

We know we can do a Write because, from package hash,

Hash is the common interface implemented by all hash functions.

type Hash interface {
// Write adds more data to the running hash.
// It never returns an error.
io.Writer

// Sum returns the current hash, without changing the
// underlying hash state.
Sum() []byte

// Reset resets the hash to one with zero bytes written.
Reset()

// Size returns the number of bytes Sum will return.
Size() int
}
and, from package io,

Writer is the interface that wraps the basic Write method.

Write writes len(p) bytes from p to the underlying data stream. It
returns the number of bytes written from p (0 <= n <= len(p)) and any
error encountered that caused the write to stop early. Write must
return a non-nil error if it returns n < len(p).

type Writer interface {
Write(p []byte) (n int, err os.Error)
}

Peter

roger peppe

unread,
May 7, 2010, 6:41:01 AM5/7/10
to B-Ranger, golan...@googlegroups.com
On 7 May 2010 07:47, B-Ranger <B-Ra...@web.de> wrote:
> So the solution is simple as I assumed but was not obvious to me,
> just for anybody having the same problem:
>
>> type Hash interface {
>>     ...
>>     io.Writer
>>     ...
>> }
>
> Does NOT say, that there is a field called "Writer" on which methods
> working on "io.Writer" work BUT it says, that this type "Hash"
> implements the methods that "io.Writer" implement. And this method(s)
> are (http://golang.org/pkg/io/#Writer):

yes, that's right. it's subtly different from an unnamed member
in a struct for that reason - in a struct, you *can* name
the member by its type.

Kevin Ballard

unread,
May 7, 2010, 3:01:22 PM5/7/10
to peterGo, golang-nuts
The confusion with WriteString() comes because io.WriteString() takes
a writer rather than being a method on Writer. So you could also use

io.WriteString(s, "SecretPasskey")

-Kevin Ballard
--
Kevin Ballard
http://kevin.sb.org
kbal...@gmail.com
Reply all
Reply to author
Forward
0 new messages