cannot find package "rsc.io/quote"

Viewed 7242

I am following the tutorial (https://golang.org/doc/tutorial/getting-started) to get started using Go and I've already run into a problem. When I run the following code:

package main

import "fmt"

import "rsc.io/quote"

func main() {
    fmt.Println(quote.Go())
}

I get the following error message in my console:

C:\Users\myname\Documents\Work\GO\hello>go run hello.go
hello.go:7:8: cannot find package "rsc.io/quote" in any of:
        C:\Program Files\Go\src\rsc.io\quote (from $GOROOT)
        C:\Users\myname\go\src\rsc.io\quote (from $GOPATH)

I am guessing this is an issue with how/ where I installed Go, can anybody shed some light please?

Thanks

3 Answers

The go tool with module support automatically downloads and installs dependencies. But for it to work, you must initialize your module.

It's not enough to save the source in a .go file and run with go run hello.go, a go.mod file must exist.

To init your module, do as indicated in the tutorial:

go mod init hello

Output should be:

go: creating new go.mod: module hello
go: to add module requirements and sums:
        go mod tidy

Starting with go 1.16, you also have to run

go mod tidy

which will download the rsc.io/quote package automatically:

go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2

So next running

go run hello.go

Will output:

Don't communicate by sharing memory, share memory by communicating.

Run this command on your command prompt:

go mod tidy

after that execute your code:

go run file_name.go

replace file_name.go with your go file example:

go run hello.go

2021/6/3 go version go1.16.4 linux/amd64

root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
        go mod tidy
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go 
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
        go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: /workspace/go_workspace/hello/go.mod already exists
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go 
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
        go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go 
Don't communicate by sharing memory, share memory by communicating.
Related