1

I am getting a compiler error "w.Write undefined (type rest.ResponseWriter has no field or method Write)"

I created a bare bones test file and have the same problem:

package server

import (
        "github.com/ant0ine/go-json-rest/rest"
)

func WriteTest(w rest.ResponseWriter) {
        var bs []byte
        w.Write(bs)
}

The method that the compiler says is not defined is definitely in the rest package.

2 Answers 2

6

The rest.ReponseWriter type has no Write, it has the following methods:

Header
WriteJson
EncodeJson
WriteHeader

However, it says in the comments that http.ResponseWriter methods are available by type assertion. So you should be able to write the following:

package server

import (
        "github.com/ant0ine/go-json-rest/rest"
        "net/http"
)

func WriteTest(w rest.ResponseWriter) {
        var bs []byte
        w.(http.ResponseWriter).Write(bs)
}
Sign up to request clarification or add additional context in comments.

Comments

2

Write is defined on responseWriter. Note the lowercase r.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.