I've have this code on a client that receives a gzipped response from an API :
client := &http.Client{}
response, _ := client.Do(r)
// Check that the server actual sent compressed data
var reader io.ReadCloser
switch response.Header.Get("Content-Encoding") {
case "gzip":
reader, err := gzip.NewReader(response.Body)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
default:
reader = response.Body
}
token, err := io.Copy(os.Stdout, reader)
if err != nil {
log.Fatal(err)
}
cadenita := strconv.FormatInt(token, 10)
fmt.Println(cadenita)
cadena := "code=b2cc1793-cb7a-ea8d-3c82-766557"
fmt.Println(cadena[5:])
But, if I use [5:] directly on cadenita, although it's also a string, I have this error.
I want to be able to slice and regex on the token(int64) transformed in a string. How can I do so ?
io.Copy returns the number of bytes copied, so that's the value thats in your token variable, so somewhere in the area of 40 for your example. FormatInt converts that to a string "40" which only has 2 chars, so it'll error as you see when you ask for the substring starting at char 5 of "40".
Are you trying to get the actual response data in token? if so you'll need to copy it into a buffer, e.g.
buff := bytes.Buffer{}
_, err := io.Copy(&buff, reader)
if err != nil {
log.Fatal(err)
}
fmt.Println(buff.String()[5:])