Import "fmt" is a program, not an importable package package command-line-arguments, imports fmt: cannot find package

hi there, i’m trying to run a very simple code, 'cause i’m pretty new in go, but i don’t understand why the command prompt shows me this message when i try to run it:
import “fmt” is a program, not an importable package, package command-line-arguments
imports fmt: cannot find package
anyone can help? please, thank you.

Go first searches for package directory inside GOROOT/src directory and if it doesn’t find the package, then it looks for GOPATH/src . Since, fmt package is part of Go’s standard library which is located in GOROOT/src , it is imported from there. But since Go cannot find greet package inside GOROOT , it will lookup inside GOPATH/src and we have it there.

//i’ve found this, i think that i’ve made a mistake cause i located goroot and gopath on the same directory, but how can i fix this?

What is your code and how do you try to run it? What is your version of go? Operating system?

1 Like

here is my code:

package main

import “fmt”

func main() {

var x = 10

var y = 15

sum := x + y

fmt.Println("La somma è ", sum)

}

i’m in this directory: C:\Users\Ilenia\go\src
i’m trying to run it with go run name of the code.go
my operating system is windows and the version of go is: go1.17.5 windows/amd64

  1. In go 1.17 you don’t need to follow GOPATH workspace convention anymore, just use a module.
  2. Make sure GOROOT points where it originally pointed to. I don’t know how it’s meant to be on windows, but on my nixos box it points into the “installation” directory.

I’ve just solved it following your instructions, thanks a lot!!!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.