-3

I am new to GO (and procedural programming) and I was working on an example code

My main.go import statement is

package main

 import (
     "fmt"
     "packagetest/mymath"
 )

 func main() {
     fmt.Println(mymath.Add(2, 3))
 }

My mymath.go package is

package mymath

func Add(a, b int) int {
    return a + b
}

func sub(a, b int) int {
    return a - b
}  

My GOPATH is C:\Users\tonyf\Desktop\go-workspace-2.0

This is where my main.go is C:\Users\tonyf\Desktop\go-workspace-2.0\src\packagetest

This is where my mymath.go is C:\Users\tonyf\Desktop\go-workspace-2.0\src\packagetest\mymath

When I run main.go i get a error like this main.go:5:2: package packagetest/mymath is not in GOROOT (C:\Program Files\Go\src\packagetest\mymath)

And problem

could not import packagetest/mymath (cannot find package "packagetest/mymath" in any of 
    C:\Program Files\Go\src\packagetest\mymath (from $GOROOT)
    C\src\packagetest\mymath (from $GOPATH)
    \Users\tonyf\Desktop\go-workspace-2.0\src\packagetest\mymath (from $GOPATH))

Please Help

Tony Frank
  • 248
  • 1
  • 2
  • 8
  • 2
    [How to Write go Code](https://golang.org/doc/code) is a step by step guide to creating and using multiple packages in a module. – JimB Mar 26 '21 at 21:11
  • 2
    The tutorial [How to Write Go Code](https://golang.org/doc/code) describes how [to use packages in your own module](https://golang.org/doc/code#ImportingLocal). – Charlie Tumahai Mar 26 '21 at 21:11
  • 1
    See also the practically-identical question https://stackoverflow.com/questions/35480623/how-to-import-local-packages-in-go – Adrian Mar 26 '21 at 21:51

1 Answers1

-1

You just need to run this command first:

go mod init <appname>

in the same application path.

Ahmed
  • 367
  • 2
  • 12