Questions about Type Writer

"type Writer

type Writer interface {
Write(p [][byte] (n int, err )
}"
Is p here an example of a variable? Or must p always be used?
Same question about n.

“Writer is the interface that wraps the basic Write method.”

What does wraps mean?

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

p (0 <= n <= len§)
I have a vague understanding of this, but I don’t have command of it.

The same for n < len§

What does underlying mean?

“Implementations must not retain p.”

What does this mean?

2 Likes
type Writer interface { Write(p [][byte] (n int, err ) }

type FooWritter struct {}

func (f *FooWritter) Write(p [][byte] (n int, err ) {  return 0, nil }

type BarWritter struct { FooWritter }

func SomeFunction(w Writter) { w.Write([]byte{1,2,3}) }


foo := new(FooWritter )
bar := new(BarWritter)

SomeFunction(foo)
SomeFunction(bar)
3 Likes

I have two answers:

  1. Technically, they’re just variable names and you could name them whatever you wish.
  2. However, for extremely common types like those from the io package, it’s often a good idea to keep the names of the return values the same p, n, and err as a convention.

I don’t think there’s a technical meaning behind the word, “wraps,” here. You could say that interfaces “bundle,” “contain” or “wrap” their member functions. The Writer type consists of a single member function: Write, so it “wraps” that one function.

(0 <= n <= len(p)) means that after Write return from being called, its n return value will be greater than or equal to 0 and less than the length of the p byte slice.

In the context of the Write function’s documentation, the “underlying stream” is the data source that you’re writing the p byte slice into. For example, *io.File implements io.Writer with its own Write member function. That member function receives the data in the p byte slice and writes that data into its “underlying stream:” the file on the disk.

It means that the Write function should not store the p value. For example, here’s a horrible io.Writer implementation that violates that documentation:

type PRetainerWriter struct {
    ps [][]byte
}

func (w *PRetainerWriter) Write(p []byte) (n int, err error) {
    // Keep p for later
    w.ps = append(w.ps, p)

    // while we're violating Write's contract, let's return invalid return values, too!
    // n is supposed to be greater than or equal to 0 or less than or equal to len(p)
    // so let's return -1!
    return -1, nil
}
3 Likes

Thanks! Very helpful. I’m going to study this again today

2 Likes

What is a data source?

Elementarily, seems like the source of the data. So would that go back to the package? Or is it something mysteriously inherent in a computer.

From Wikipedia… DataSource is a name given to the connection set up to a database from a server
To me, a connection is something electrical.

I know that means a slice of bytes, but what is byte

My information says that the syntax of functons is: func (receiver) identifier(parameters) (returns) { code }

So in :

func (w *PRetainerWriter) Write(p []byte) (n int, err error) {
    // Keep p for later
    w.ps = append(w.ps, p)

    // while we're violating Write's contract, let's return invalid return values, too!
    // n is supposed to be greater than or equal to 0 or less than or equal to len(p)
    // so let's return -1!
    return -1, nil
}

would (w *PRetainerWriter) be a receiver?
Write an identifier?
(p byte) a parameter?
and the rest the code?

I’m trying to understand what a receiver is. Using this code, Go Playground - The Go Programming Language, my teacher meade the comment that “bar takes in a string”. So in that code, is (s string) a receiver? And does that mean that bar takes in any string that is passed by bar?

More questions about functions. Hope you don’t mind that I’ve changed subjects. Somehow it relates.

In: * Go Playground - The Go Programming Language
is string a return?
Why is it there, and not here: func bar(s string) {

2 Likes

I reviewed your reply, an it’s very helpful

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.