educative.io

Package book/pack1 is not in GOROOT (C:\Program Files\Go\src\book\pack1)

I’m following the book/pack1 example and getting the error:
package book/pack1 is not in GOROOT (C:\Program Files\Go\src\book\pack1)
when trying to run “go install book/pack1”.

go version go1.19 windows/amd64

These are details from go env:
set GOPATH=C:\Users\gangr\go
set GOROOT=C:\Program Files\Go

This is the path to pack1.go:
“C:\Users\gangr\go\src\book\pack1\pack1.go”

why wouldn’t the package search be done in GOPATH? and fail right away with GOROOT related error?

Hi @Igor4,
Below is the list of possible solutions, hope that helps.

  • First update to the most recent Go version 1.20 (although 1.19 should probably be good)

  • Then try out this command: go env -w GO111MODULE=off

Also check the package has only one go.mod file.

  • The user has his package stored in “C:\Users\gangr\go\src\book\pack1\pack1.go”,

then main.go in scr/book/book_main should be changed to:

package main

import ( "fmt" "pack1.go/book/pack1")

func main() { var test1 string test1 = pack1.ReturnStr() fmt.Printf("ReturnStr from package1: %s\n", test1) fmt.Printf("Integer from package1: %d\n", pack1.Pack1Int) //fmt.Printf("Float from package1: %f\n", pack1.pack1Float)}