204

I'm trying to create and use a custom package in Go. It's probably something very obvious but I cannot find much information about this. Basically, I have these two files in the same folder:

mylib.go

package mylib

type SomeType struct {

}

main.go

package main

import (
    "mylib"
)

func main() {

}

When I try to go run main.go, I get this error:

main.go:4:2: import "mylib": cannot find package

I've tried to run go build mylib.go first but it doesn't seem to be doing anything (no file generated, no error message). So any idea how I could do this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
laurent
  • 88,262
  • 77
  • 290
  • 428

9 Answers9

188

First, be sure to read and understand the "How to write Go code" document.

The actual answer depends on the nature of your "custom package".

If it's intended to be of general use, consider employing the so-called "Github code layout". Basically, you make your library a separate go get-table project.

If your library is for internal use, you could go like this:

  1. Place the directory with library files under the directory of your project.
  2. In the rest of your project, refer to the library using its path relative to the root of your workspace containing the project.

To demonstrate:

src/
  myproject/
    mylib/
      mylib.go
      ...
    main.go

Now, in the top-level main.go, you could import "myproject/mylib" and it would work OK.

Edson Medina
  • 9,862
  • 3
  • 40
  • 51
kostix
  • 51,517
  • 14
  • 93
  • 176
  • 1
    If I create a new project (myproject2) under src/, how could I import mylib? – Kiril Apr 14 '14 at 13:04
  • 3
    @Kiril, you mean, how do you import `mylib` in the code of `myproject2`? Then the answer is "by using `import "myproject/mylib"` -- the idea is that Go searches for imported paths under each directory it extracts from the `GOPATH` environment variable (they are called "workspaces"), but this search is (luckily) *not recursive,* so such paths are effectively "anchored" at their respective workspaces. – kostix Apr 14 '14 at 17:00
  • 4
    Another must-have read: ["Package names"](https://blog.golang.org/package-names). – kostix Dec 06 '15 at 10:02
  • This answer is ambiguous in that there are two `mylib` items listed and it's not clear if this is a requirement or just a simplification of the example. If you could clarify in the answer it would be more useful. – cmcginty Aug 29 '17 at 02:58
  • I have several microservices which all depend on the same mylib. If I change mylib, I would have to change it in all services. How would you handle this? – Matthias Sommer May 15 '19 at 12:50
  • 1
    @MatthiasSommer, typically—by extracting that mylib into a common package each microservice uses. Exactly how "uses" is defined, depends on your preferred workflow. In enterprise-grade development, [vendoring](https://golang.org/cmd/go/#hdr-Vendor_Directories) is typically used, but with the recent `go mod` developments, [a module](https://blog.golang.org/using-go-modules) might be the answer (and `go mod` supports vendoring of modules as well). – kostix May 15 '19 at 15:33
  • so the package to be imported can only be .go file? how to build package to .lib file then import the lib file? – Lei Yang Aug 29 '19 at 04:43
  • 1
    @LeiYang, "the package to be imported" is a directory with one or more Go source files. As to "how to build package to a .lib file" and then import the lib file"—the answer depends on what you really intended to ask for. If you're concerned with compilation speed, then fear not: the Go toolchain caches all build results on a per-package basis. If, instead, you wanted to ask whether it's possible to compile _and distribute a binary-only compiled package,_ then the answer is [no](https://tip.golang.org/doc/go1.12#binary-only). – kostix Aug 30 '19 at 08:48
  • 1
    @LeiYang, but note that if for some reason you still desperately need something like this, on some platforms Go supports [plugins](https://golang.org/pkg/plugin/). – kostix Aug 30 '19 at 08:49
78

For this kind of folder structure:

main.go
mylib/
  mylib.go

The simplest way is to use this:

import (
    "./mylib"
)
laurent
  • 88,262
  • 77
  • 290
  • 428
  • 4
    This does not work anymore in recent versions of go as the package will not be found. The correct import would be `foo/mylib` (assuming the folder containing main.go is `foo`). – nemo Aug 27 '13 at 00:51
  • 6
    @nemo, with the latest version of go, I always use "./mylib" and it works. – laurent Aug 27 '13 at 03:49
  • 3
    Using go 1.2 and I agree with @this.lau_ – canadadry Mar 17 '14 at 20:30
  • 8
    Be aware that this makes `go install` break. If you're building a standalone project that you want people to download and run `go build` on, this is fine--however, I would employ the "Github code layout" mentioned above (even if off bitbucket, or similar) if you want full `go install` support. – photoionized Apr 10 '14 at 17:24
  • I suggestionDo not use this way. It's will breaking godef. godef it does not understand about "." imports – King Jk Mar 02 '17 at 07:11
8

I am an experienced programmer, but, quite new into Go world ! And I confess I've faced few difficulties to understand Go... I faced this same problem when trying to organize my go files in sub-folders. The way I did it :

GO_Directory ( the one assigned to $GOPATH )

GO_Directory //the one assigned to $GOPATH
__MyProject
_____ main.go
_____ Entites
_____ Fiboo // in my case, fiboo is a database name
_________ Client.go // in my case, Client is a table name

On File MyProject\Entities\Fiboo\Client.go

package Fiboo

type Client struct{
    ID int
    name string
}

on file MyProject\main.go

package main

import(
    Fiboo "./Entity/Fiboo" 
)

var TableClient  Fiboo.Client

func main(){
    TableClient.ID = 1
    TableClient.name = 'Hugo'

    // do your things here
}

( I am running Go 1.9 on Ubuntu 16.04 )

And remember guys, I am newbie on Go. If what I am doing is bad practice, let me know !

Taavi
  • 135
  • 2
  • 16
Diego Favero
  • 1,969
  • 2
  • 22
  • 32
6

For a project hosted on GitHub, here's what people usually do:

github.com/
  laike9m/
    myproject/
      mylib/
        mylib.go
        ...
      main.go

mylib.go

package mylib

...

main.go

import "github.com/laike9m/myproject/mylib"

...
laike9m
  • 18,344
  • 20
  • 107
  • 140
3

I try so many ways but the best I use go.mod and put

module nameofProject.com

and then i import from same project I use

import("nameofProject.com/folder")

It's very useful to create project in any place

2

another solution:
add src/myproject to $GOPATH.

Then import "mylib" will compile.

Helin Wang
  • 4,002
  • 1
  • 30
  • 34
1

finally I found the solution for this problem. Please note I have a very basic coding environment, also using basic stuff as I am a beginner at the moment. I wanted to use my library in the very same way as I use official libraries. I didn't have to edit GOPATH or any other system variables or paths, no need to rebuild or install anything. So here is my solution from top-down:

  1. In my main package I have my code as follows:
    package main
    
    import (
      "mylib"  // This is my library!!
      "fmt"
    )
    
    
    func main() {
      ...
      mylib.myFunc(par)
      mylib.myOtherFunc(par1,par2)
      ...
    }
  1. You have to use the very same package name in the package implementation files. My own package looks like as follows:
package mylib

import (
  "math"
  "strconv"
  "strings"
)

func myFunc(par int) {
  ...
}

func myOtherFunc(par1 string, par2 int) {
  ...
}
  1. What you have to keep in mind to name and place correctly all the stuff:
  • Create a subfolder with the package name next to the other packages. You can find them easily by searchin eg. 'bufio', 'os' or 'math' (on my machine they are under \Go\src). In my case it looks like this:

     C:
      /Go
        /src
          /bufio
          /io
          /...
          /mylib
          /...
          /strings
    
  • Move your package file into this folder. The folder name is important, this has to be the same as the package name, this is how your compiler finds it

  • You can have one ore more .go files here, they needn't to be named mylib.go, you can use any names, eg. myMath.go, myString.go, etc.

  • I repeat: the package name in all the .go files has to be your library name ('mylib' in my case)

  1. This way later on you can extend your own library by new functions, they can be implemented in separate .go files, the only important is you have to store this file in subfolder named 'mylib' and the package name has to be the same.

I hope it helps, best regards,

LA

0

For those who face this problem, you need to first initialize the go module before you can use custom package.

For example your code directory is: ../mycode/main.go. Now you want to create diffcode custom package and import it into main. You will first need to run the command go mod init mycode (make sure you are under ../mycode directory). Now you create package diffcode and it has some files. To import this package, you need to put this into main.go: module/package. In this case, mycode/diffcode.

s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
0

The essential question here is to tailor the idea of compiling the package as library binary, and then import the binary code as third party library like "net/http" or "fmt" in some new go project, instead using the original go code. I am wondering if this is possible or not for go programming.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 17 '21 at 06:55
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29848290) – Subhashis Pandey Sep 17 '21 at 07:20