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
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.
Related
I'm using the following Go module to build an application:
github.com/martinlindhe/unit
Right now, the Go code is very simple; I'm just trying to get the environment set up for the real work:
import(
"fmt"
"unit"
)
foo := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())
In go.mod:
go 1.17
require github.com/martinlindhe/unit v0.0.0-20210313160520-19b60e03648d
Executing either go build or go get results in:
package unit is not in GOROOT (/usr/local/Cellar/go/1.17/libexec/src/unit)
Running go mod download executes without error. The go.sum file seems to be correct, all the necessary dependencies exist.
The environment is the latest version of VS Code, Go installed via homebrew on MacOS Big Sur 11.5.2
There must be something obvious I'm missing. I've not had this problem with other apps I've written.
The import path is not right. Playground.
package main
import (
"fmt"
"github.com/martinlindhe/unit"
)
func main() {
foo := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())
}
I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly?
Package live here locally:
../goworkspace/src/github.com/garrettlove8/goserve
Import from other module:
...
import (
"fmt"
"io"
"net/http"
"github.com/garrettlove8/goserve" // I have tried "goserve" and "../goserve"
)
...
go.mod:
...
require github.com/garrettlove8/goserve v0.1.17
No matter what I do it doesn't seem to work as desired
Update
Code and error combos:
// main.go
import (
"fmt"
"io"
"net/http"
"goserve"
)
Run go mod tidy
// go.mod becomes
require github.com/garrettlove8/goserve v0.1.17 // indirect
Error:
goserve: package goserve is not in GOROOT (/usr/local/go/src/goserve)
Manually changing go mod to this (which I don't to have to do):
require github.com/garrettlove8/goserve
Run go mod tidy
Error:
usage: require module/path v1.2.3
When you want to use a local module instead of a remote one, you can achieve that with the replace directive.
In your case, add this to your go.mod file:
replace github.com/garrettlove8/goserve => ../goworkspace/src/github.com/garrettlove8/goserve
I am following this tutorial to publish a topic to Pub/Sub from a golang project and here's the code I have for that project at the moment:
package main
import "cloud.google.com/go/pubsub"
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
All it does is simply imports the pubsub but when I run go get I get this error: undefined: ocgrpc.NewClientStatsHandler
C:\Users\iha001\Dev\golang-projects\src\github.com\naguibihab\golang-playarea\src\gcloud>go get
# cloud.google.com/go/pubsub
..\..\..\..\..\cloud.google.com\go\pubsub\go18.go:34:51: undefined: ocgrpc.NewClientStatsHandler
Is there anything else I need to install to get this running?
I was having the same issue on mac, using "cloud.google.com/go/pubsub" version 0.19.0. The fix for me was bumping the version down to 0.18.0.
It seems to have been an issue on the repo:
#naguibihab This is NOT a windows issue. This commit fixes the problem
be072a5. Short explanation: breaking changes where pushed on a minor
release of a google pubsub dependency:
census-instrumentation/opencensus-go#ac82455, method
NewClientStatsHandler was deleted. (They don't claim anywhere they
comply with semver).
Here's the fix mentioned in that comment: https://github.com/GoogleCloudPlatform/google-cloud-go/commit/be072a5d1d73144ae0ce1071e9bd43d1ad221581
I'm trying to make a program dependent upon gcimporter15 for Go, and so I'm using the command "go get golang.org/x/tools/go/gcimporter15", but it fails with the error:
# golang.org/x/tools/go/gcimporter15
../../go/src/golang.org/x/tools/go/gcimporter15/bexport.go:557: undefined: constant.ToFloat
../../go/src/golang.org/x/tools/go/gcimporter15/gcimporter.go:396: pkg.SetName undefined (type *types.Package has no field or method SetName)
That appears to me that there is an error within gcimporter itself, but that doesn't make sense that I would get this when it doesn't appear that others are. Why isn't it working?
I'm using Go 1.5.3.
The godoc.org/golang.org/x/tools/go/gcimporter15 tells that the package gcimporter is deprecated and this package will be deleted in October 2017. And this new code should be used: golang.org/x/tools/go/gcexportdata instead of gcimporter .
So, what you have to do is:
$ go get godoc.org/golang.org/x/tools/go/gcexportdata
PS: Tested within Ubuntu 16.04 64bit and go version go1.6.2 linux/amd64 without any issues.
Test:
package main
import (
"fmt"
gcexportdata "golang.org/x/tools/go/gcexportdata"
)
func main() {
filename, path := gcexportdata.Find("fmt", "")
fmt.Println(filename, path)
}
Output:
/usr/lib/go-1.6/pkg/linux_amd64/fmt.a fmt
Its bad but seems there is a mismatch between the gcimporter15 and go-1.5. I faced the same problem and looked at go-1.5 code in file src/go/constant/value.go and there is no ToFloat() function in the constant package.
As #nexus66 says, the gcimporter is deprecated. So, I don't expect them to fix this. If you are using this package directly, its better to move on to the recommended gcexportdata package. If you are using a third party library which is in turn using gcimporter, may be you should upgrade to go-1.7. That's what I did and things worked fine.
I want to step through my program using Godebug. However because I'm using net/http I get errors such as:
/home/heath/go/src/net/http/h2_bundle.go:45:2: could not import golang_org/x/net/http2/hpack (cannot find package "golang_org/x/net/http2/hpack" in any of:
/home/heath/go/src/golang_org/x/net/http2/hpack (from $GOROOT)
/x/net/http2/hpack does exist in my GOPATH but in ~heath/go/src/golang.org ... not golang_org (not sure what's happening there)
I have read that this error occurs because godebug doesn't support http2 yet (can't find source).
I have tried to disable http2server and http2client by setting the GODEBUG env both in init() and on the command line. I have also confirmed that these settings are being set by doing a fmt.Println("GODEBUG", os.Getenv("GODEBUG"). As per instructions located here
GODEBUG=http2client=0 # disable HTTP/2 client support
GODEBUG=http2server=0 # disable HTTP/2 server support
My simple code example to replicate the error is:
package main
import "fmt"
import "net/http"
import "os"
func init() {
os.Setenv("GODEBUG", "http2server=0,http2client=0")
}
func main() {
fmt.Println("GODEBUG", os.Getenv("GODEBUG"))
_ = "breakpoint"
fmt.Println("Hello, World!")
http.ListenAndServe(":8080", nil)
}
godebug run example.go
I am running Go version:
go version go1.7 linux/amd64
This is probably not the correct way to do things. But I copied the http2 package from my vendor directory under $GOROOT/src/vendor to the src directory under my $GOPATH/src.
If anyone has any further input on the correct way to reference vendor directories please add your thoughts. Just putting my workaround here in case someone else comes across the same issue.
Edit: Actually a 'nicer' way to do things was to do an ln -s ..src/vender/github_org to ../src/github_org