ReadAll 未由包 iocompiler 声明 - Golang
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 not declared by package iocompiler
知道如何解决这个问题吗?
编辑:我使用的go版本是
go version
go version go1.15.6 darwin/amd64
作为 Manjeet and Emile stated, starting from go1.16
's release, the functions from io/ioutil are moved to 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
的 release notes 以供参考。
关于go版本不匹配问题:go1.13
in go.mod
and go1.15
in terminal, go.mod
中的go版本是根据当时的版本添加的使用 go mod init [module-path]
初始化。 go mod init 的 GoDoc 页面无法消除您的疑问,但附上参考
换句话说,go.mod
并不表示您的本地 go 版本。相反,它提供了最小的 go 版本和导入包的版本,这将使您的项目(作为模块)运行 hassle-free.
我从一个旧项目中找到了一个示例。将尝试解释它。
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.0
、testify@v1.6.1
和 mockery@v1.1.2
。低于这些版本“可能”仍然有效。不建议。高于那些版本预计会更好,但有可能像上面那样更改名称或包名称。更多详情请见 gomod-ref.
还有一点信息:io
和 io/util
是 Go 的 standard library 的一部分。这意味着为了将函数用作 io.ReadAll
,您需要更高版本的标准库,因此需要更高版本的 Go(至少 1.16
)。
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!
意见: 我个人目前推荐 go1.17
的最新版本,直到所有外部包实现 go1.18
和 generics
.
我在我的项目中使用了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 not declared by package iocompiler
知道如何解决这个问题吗?
编辑:我使用的go版本是
go version
go version go1.15.6 darwin/amd64
作为 Manjeet and Emile stated, starting from go1.16
's release, the functions from io/ioutil are moved to 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
的 release notes 以供参考。
关于go版本不匹配问题:go1.13
in go.mod
and go1.15
in terminal, go.mod
中的go版本是根据当时的版本添加的使用 go mod init [module-path]
初始化。 go mod init 的 GoDoc 页面无法消除您的疑问,但附上参考
换句话说,go.mod
并不表示您的本地 go 版本。相反,它提供了最小的 go 版本和导入包的版本,这将使您的项目(作为模块)运行 hassle-free.
我从一个旧项目中找到了一个示例。将尝试解释它。
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.0
、testify@v1.6.1
和 mockery@v1.1.2
。低于这些版本“可能”仍然有效。不建议。高于那些版本预计会更好,但有可能像上面那样更改名称或包名称。更多详情请见 gomod-ref.
还有一点信息:io
和 io/util
是 Go 的 standard library 的一部分。这意味着为了将函数用作 io.ReadAll
,您需要更高版本的标准库,因此需要更高版本的 Go(至少 1.16
)。
Conclusion: You can still keep
go1.13
ingo.mod
. Just upgrade the go version in local. And if other devs are kind enough to let you upgrade ingo.mod
too, do it!
意见: 我个人目前推荐 go1.17
的最新版本,直到所有外部包实现 go1.18
和 generics
.