Package io/fs is not in goroot

Error: package io/fs is not in GOROOT

This error occurs when you try to import the package “io/fs” in your Go code, but the package is not found in the GOROOT directory.

Explanation

The GOROOT environment variable is used by Go to locate the root directory of the Go installation. It contains the standard library packages and other essential files required for Go to run.

The package “io/fs” was introduced in Go 1.16 to provide a file system abstraction. However, if you are using an older version of Go, this package may not be available in your GOROOT.

Example:

Let’s assume you have the following Go code that tries to import the “io/fs” package:


package main

import (
"fmt"
"io/fs" // error: package io/fs is not in GOROOT
)

func main() {
fmt.Println("Hello, world!")
}

If you try to compile and run this code, you will encounter the mentioned error. This is because the “io/fs” package is not available in your GOROOT.

To resolve this error, you need to update your Go version to at least 1.16, where the “io/fs” package was introduced. Here’s how you can do it:

  • Download the latest Go version from the official Go website (https://golang.org/dl/).
  • Install the downloaded Go package following the installation instructions specific to your operating system.
  • After installing, make sure to update your GOROOT environment variable to point to the newly installed Go version.
  • Now, try compiling and running your code again, and the error should be resolved.

Once you have the updated Go version, you will have access to the “io/fs” package and its functionalities.

Read more

Leave a comment