ReadAll在Golang中未被iocompiler包声明。

huangapple go评论84阅读模式
英文:

ReadAll not declared by package iocompiler - Golang

问题

我在我的项目中使用Golang。

代码如下:

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"sync"
	"time"
)

...

if response.StatusCode != 200 {
	respBdy, _ := io.ReadAll(response.Body)
	return fmt.Errorf("%v response from client: %v", response.StatusCode, respBdy)
}

当我使用make run运行项目时,我遇到了以下错误:

go-tools@v0.0.0-20220528203058-9108e3643722/messengers/client.go:133:17: undefined: io.ReadAll

经过调试,我可以理解问题是ReadAll未在io包中声明。

有什么办法可以解决这个问题吗?

编辑:我使用的Go版本是:

go version
go version go1.15.6 darwin/amd64
英文:

I am using golang in my project.

The code is as follows

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"sync"
	"time"

)

....
if response.StatusCode != 200 {
		respBdy, _ := io.ReadAll(response.Body)
		return fmt.Errorf("%v response from client: %v", response.StatusCode, respBdy)
	}

When i run the project using make run, i am getting the following error

go-tools@v0.0.0-20220528203058-9108e3643722/messengers/client.go:133:17: undefined: io.ReadAll

On debugging, i can understand the problem is ReadAll not declared by package iocompiler

Any idea on how to fix this?

Edit: The go version i am using is

go version
go version go1.15.6 darwin/amd64

答案1

得分: 0

根据ManjeetEmile的说法,在go1.16版本发布后,io/ioutil中的函数被移动到io包中。

Discard => io.Discard
NopCloser => io.NopCloser
ReadAll => io.ReadAll
ReadDir => os.ReadDir
ReadFile => os.ReadFile
TempDir => os.MkdirTemp
TempFile => os.CreateTemp
WriteFile => os.WriteFile

链接发布说明以供参考go1.16

关于go版本不匹配的问题:go.mod中是go1.13,终端中是go1.15go.mod中的go版本是根据使用go mod init [module-path]初始化时的版本添加的。go mod init的GoDoc页面并没有解答你的疑问,但附上供参考。

换句话说,go.mod并不表示你的本地go版本。相反,它提供了让你的项目(作为模块)无缝运行所需的最低go版本和导入包的版本。

我找到了一个旧项目的示例,将尝试对其进行解释。

module project-name

go 1.14

require (
	github.com/go-redis/redis/v7 v7.4.0
	github.com/stretchr/testify v1.6.1
	github.com/vektra/mockery v1.1.2 // indirect
)

这意味着该项目需要go1.14,以及至少需要redis@v7.4.0testify@v1.6.1mockery@v1.1.2。低于这些版本的可能仍然可以工作,但不建议这样做。高于这些版本预计会更好,但可能会更改名称或包名,就像上面的示例一样。更多详细信息请参阅gomod-ref

还有一点信息:ioio/util是Go的标准库的一部分。这意味着要使用io.ReadAll这样的函数,您需要更高版本的标准库,因此需要更高版本的Go(至少是1.16)。

**结论:**您仍然可以在go.mod中保留go1.13。只需升级本地的go版本。如果其他开发人员也愿意让您在go.mod中升级,那就这样做吧!

**意见:**我个人建议目前使用go1.17的最新版本,直到所有外部包都实现了go1.18generics

英文:

As Manjeet and Emile stated, starting from go1.16's release, the functions from io/ioutil are moved to io package.

Discard => io.Discard
NopCloser => io.NopCloser
ReadAll => io.ReadAll
ReadDir => os.ReadDir
ReadFile => os.ReadFile
TempDir => os.MkdirTemp
TempFile => os.CreateTemp
WriteFile => os.WriteFile

Linking release notes of go1.16 for reference.

About the mismatch of go versions: go1.13 in go.mod and go1.15 in terminal, the go version in go.mod gets added depending on the version when it was initialised using go mod init [module-path]. GoDoc page for go mod init doesn't clear your doubt but attaching for ref.

In other words, the go.mod doesn't denote your local go version. Rather, it gives the minimal go version and imported package's versions that will let your project (as module) run hassle-free.

I found a sample from an old project. Will try to explain on it.

module project-name

go 1.14

require (
	github.com/go-redis/redis/v7 v7.4.0
	github.com/stretchr/testify v1.6.1
	github.com/vektra/mockery v1.1.2 // indirect
)

This means the project requires go1.14, along with redis@v7.4.0, testify@v1.6.1 and mockery@v1.1.2 at the very least. Lower than these versions "might" still work. Not recommended. Higher than those versions are expected to be better, but there is a possibility of change of names or package name like above. More details at gomod-ref.

Just another bit of info: the io and io/util are part of Go's standard library. This means that in order to use the function as io.ReadAll, you would need higher version of standard library, and so, higher version of Go (1.16 at least).

> Conclusion: You can still keep go1.13 in go.mod. Just upgrade the go version in local. And if other devs are kind enough to let you upgrade in go.mod too, do it!

Opinion: I personally recommend go1.17's latest for now until all external packages implement go1.18 and generics.

huangapple
  • 本文由 发表于 2022年5月30日 11:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/72428889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定