Convert io.ReadCloser to a String

Net/http is an amazing package but there are times you need to work with the response of call you have just made. Many functions require a string as input so we have to convert it first by passing it through a buffer.

package main

import (

func main() {
	response, _ := http.Get("")

	// This would fail because Body = io.ReadCloser
	// fmt.Printf(response.Body)

	// we convert it to a string by passing it through 
	// a buffer first. A 'costly' but useful process.
	buf := new(bytes.Buffer)
	newStr := buf.String()


Leave a Reply

Your email address will not be published. Required fields are marked *