I made a couple smaller Go apps and studied Go books. But I still struggle tremendously with Go’s type and the different conversion paths between them. How can I learn those and code confidently, without having to copy/paste online code?
Here’s an example to illustrate my struggles. In my app I’m working with NewDecoder()
, which takes an io.Reader
.
I make a HTTP request with http.Get()
, which seems to return an Response
if I understand the docs right. The Body
field of that type returns io.ReadCloser
.
So to make both work, I need to go from io.ReadCloser
→ io.Reader
. But where do I find that function?
I found here that no conversion path is needed for io.ReadCloser
→ io.Reader
. But my NewDecoder()
errors with “unexpected EOF” when I give it Response.Body
, so I probably need a reader in between them that can properly handle EOF without erroring.
Earlier I made NewDecoder
work with os.Open()
, which returns a File
according to the docs. But that doc page doesn’t define the struct’s field, and I couldn’t find online what type File.Body
is.
Edit: I “fixed” the problem in this specific case by putting a new(bytes.Buffer)
and ReadFrom()
function between the HTTP request and the NewDecoder()
.
This is of course not a real solution since I only found it through trail & error, copying online code, a lot of time (1+ hour), and a decent bit of frustration.
So how do I learn Go’s types and the conversion paths, so I have a much more pleasant and productive Go experience?