By this.lau_

2013-02-24 08:15:47 8 Comments

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:


package mylib

type SomeType struct {



package main

import (

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?


@Diego Favero 2017-12-24 18:11:44

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
_____ 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

    Fiboo "./Entity/Fiboo" 

var TableClient  Fiboo.Client

func main(){
    TableClient.ID = 1 = '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 !

@Hexagendedor 2017-11-09 02:07:41

package lexico

type Analizador struct {


func main() {


En la clase package main

import (


@laike9m 2017-07-06 15:08:56

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


package mylib



import ""


@kostix 2013-02-24 11:14:18

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:


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

@Kiril 2014-04-14 13:04:23

If I create a new project (myproject2) under src/, how could I import mylib?

@kostix 2014-04-14 17:00:18

@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 2015-12-06 10:02:27

Another must-have read: "Package names".

@cmcginty 2017-08-29 02:58:41

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.

@this.lau_ 2013-08-13 05:27:37

For this kind of folder structure:


The simplest way is to use this:

import (

@nemo 2013-08-27 00:51:12

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).

@this.lau_ 2013-08-27 03:49:19

@nemo, with the latest version of go, I always use "./mylib" and it works.

@canadadry 2014-03-17 20:30:32

Using go 1.2 and I agree with @this.lau_

@photoionized 2014-04-10 17:24:04

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.

@King Jk 2017-03-02 07:11:17

I suggestionDo not use this way. It's will breaking godef. godef it does not understand about "." imports

@Helin Wang 2014-07-22 16:14:35

another solution:
add src/myproject to $GOPATH.

Then import "mylib" will compile.

@Gustav 2016-04-28 10:50:30

this will not work for go gettable paths such as github hosted packages.

Related Questions

Sponsored Content

53 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

9 Answered Questions

[SOLVED] Relative imports for the billionth time

40 Answered Questions

[SOLVED] How to import an SQL file using the command line in MySQL?

1 Answered Questions

[SOLVED] How to access a struct from an external package in Go

  • 2016-09-26 19:10:22
  • Colleen Larsen
  • 2025 View
  • 2 Score
  • 1 Answer
  • Tags:   go struct import

1 Answered Questions

[SOLVED] golang struct from other package

  • 2017-07-28 01:27:16
  • glyif
  • 1306 View
  • 1 Score
  • 1 Answer
  • Tags:   go struct

20 Answered Questions

[SOLVED] Find the version of an installed npm package

  • 2012-06-10 20:36:27
  • Laurent Couvidou
  • 882178 View
  • 1767 Score
  • 20 Answer
  • Tags:   node.js package npm

1 Answered Questions

[SOLVED] Trying to understand how to find structs in my project and packages

  • 2018-02-06 01:59:18
  • Blankman
  • 31 View
  • -2 Score
  • 1 Answer
  • Tags:   go

10 Answered Questions

[SOLVED] How to find out which package version is loaded in R?

  • 2012-06-19 14:30:29
  • mpiktas
  • 259895 View
  • 317 Score
  • 10 Answer
  • Tags:   r package version

1 Answered Questions

[SOLVED] Golang invalid import path when importing from "main" folder

2 Answered Questions

[SOLVED] GoLang and packages layout with relative paths

  • 2016-05-18 22:15:32
  • Roberto Milani
  • 949 View
  • 1 Score
  • 2 Answer
  • Tags:   go package packages

Sponsored Content