cannot find package "rsc.io/quote"
Asked Answered
P

7

7

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

Petal answered 20/10, 2020 at 9:21 Comment(0)
J
48

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.
Jackson answered 20/10, 2020 at 9:27 Comment(5)
Thank you! I see, the go.mod file is a way of keeping track of the .go file and its dependencies.Petal
Doesn't work. Still get the same error message: go: finding module for package rsc.io/quote hello.go:5:8: module rsc.io/quote: Get "proxy.golang.org/rsc.io/quote/@v/list": proxyconnect tcp: dial tcp: lookup https: no such hostFiddlededee
@AndrewGray that doesn't look like the same error message as it seems to be talking about networking conceptsEarthshaking
"Don't communicate by sharing memory, share memory by communicating." is it valid output?Frazil
@Frazil That's what the end of the answer tells, isn't it?Jackson
I
2

For anyone using VSCode or anything else that auto-lints

  1. I added

    import "rsc.io/quote"

  2. I had not yet used the

    quote.Go()

  3. On file save, the linter removed the import.

  4. Go mod tidy had nothing to do, so when I retyped it, it was still flagged as broken.

I think I would vastly prefer to check that a package can be imported before actually coding any calls to it. I can't think off the top of my head of any other linter that behaves like this by default.

Beginners (which Go is aimed at) may also not think of this. Of course beginners may follow the tutorial steps EXACTLY and not try to import a package and not use it before saving the file! :-)

Inequity answered 19/3, 2024 at 14:37 Comment(0)
E
2

Thanks to tip from Kpollock above, anyone using VScode (or another auto-linting IDE), please run the following command in terminal;

go get rsc.io/quote

THEN add the import and modify the fmt.Println() statement to add the quote.Go() function.

After these two steps, you will be able to save the file without auto-linter removing the import.

Exine answered 31/5, 2024 at 21:25 Comment(0)
P
1

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

Pontias answered 2/9, 2021 at 14:35 Comment(0)
I
0

The answer by icza is perfect although I would like to add one tiny suggestion to it. performing,

go mod tidy

installs the missing packages you need in the program if you already have a go.mod file. it simply ensures all the imports are satisfied in your current program. so whenever you import a new package and the error says no module found, dont make the rookie mistake of creating a whole new dir for the program and then initializing it with go mod init , instead, run the above command and you should be good to go

Impermissible answered 16/2, 2023 at 15:53 Comment(0)
R
0

I had the same problem with version 1.22.2, just go to your path (cmd) and use this command:

$ go get rsc.io/quote
Rising answered 2/5, 2024 at 2:39 Comment(0)
S
-1

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.
Sayette answered 3/6, 2021 at 11:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.