laurent
laurent

Reputation: 90736

How to use custom packages

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?

Upvotes: 208

Views: 210943

Answers (9)

user19535281
user19535281

Reputation: 11

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

Upvotes: 1

user16934160
user16934160

Reputation: 1

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.

Upvotes: 0

Helping Hand
Helping Hand

Reputation: 1

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.

Upvotes: 0

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

Upvotes: 5

Diego Favero
Diego Favero

Reputation: 2105

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 !

Upvotes: 9

laike9m
laike9m

Reputation: 19318

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"

...

Upvotes: 6

kostix
kostix

Reputation: 55443

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.

Upvotes: 190

laurent
laurent

Reputation: 90736

For this kind of folder structure:

main.go
mylib/
  mylib.go

The simplest way is to use this:

import (
    "./mylib"
)

Upvotes: 79

Helin Wang
Helin Wang

Reputation: 4202

another solution:
add src/myproject to $GOPATH.

Then import "mylib" will compile.

Upvotes: 2

Related Questions