All Questions
330
questions
225
votes
9
answers
135k
views
How can I "go run" a project with multiple files in the main package?
I have a single file in the main package called main.go. Because the code isn't reusable I want to separate part of the code in a different file but in the same package.
How do I split the contents of ...
222
votes
10
answers
331k
views
How to import local packages without gopath
I've used GOPATH but for this current issue I'm facing it does not help. I want to be able to create packages that are specific to a project:
myproject/
├── binary1.go
├── binary2.go
├── package1.go
└...
208
votes
9
answers
206k
views
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
...
192
votes
2
answers
91k
views
How to import and use different packages of the same name
For example, I want to use both text/template and html/template in one source file.
But the code below throw errors.
import (
"fmt"
"net/http"
"text/template" // template redeclared as ...
191
votes
16
answers
444k
views
Go build: "Cannot find package" (even though GOPATH is set)
Even though I have GOPATH properly set, I still can't get "go build" or "go run" to find my own packages. What am I doing wrong?
$ echo $GOROOT
/usr/local/go
$ echo $GOPATH
/home/mitchell/go
$ cat ~...
181
votes
5
answers
245k
views
Go update all modules
Using this module as an example (using a specific commit so others will see
what I see):
git clone git://github.com/walles/moar
Set-Location moar
git checkout d24acdbf
I would like a way to tell Go ...
160
votes
15
answers
294k
views
How do I import a specific version of a package using go get?
coming from a Node environment I used to install a specific version of a vendor lib into the project folder (node_modules) by telling npm to install that version of that lib from the package.json or ...
119
votes
2
answers
128k
views
How to build executable with name other than Golang package
Is it possible to build (install, go get, etc) an executable with the name foobar if my Golang package name is one of the following:
github.com/username/go-foobar
github.com/username/foobar-tools
...
92
votes
6
answers
105k
views
Non-declaration statement outside function body in Go
I'm building a Go library for an API that offers JSON or XML formatted data.
This API requires me to request a session_id every 15 minutes or so, and use that in calls. For example:
foo.com/api/[my-...
79
votes
15
answers
167k
views
Go failing - expected 'package', found 'EOF'
I've been having a hard time trying to execute a simple golang program in a virtual machine powered by vagrant.
These are the relevant fields of my go env:
GOARCH="amd64"
GOPATH="/usr/local/src/go"
...
71
votes
3
answers
139k
views
Import struct from another package and file golang
I have a problem trying to import a type from another package and file. The struct that I'm trying to import is the one underneath.
type PriorityQueue []*Item
type Item struct {
value string
...
71
votes
4
answers
83k
views
Does it make sense to have two packages in the same directory?
I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).
Example of directory structure:
whatever.io/
...
70
votes
5
answers
72k
views
Go, go get, go install, local packages, and version control
I am having trouble understanding the workflow for creating a go project that has local packages.
Say I create a new project, using git for version control, which has a main.go file and a tools.go ...
68
votes
12
answers
89k
views
How to use Go with a private GitLab repo
GitLab is a free, open-source way to host private .git repositories but it does not seem to work with Go. When you create a project it generates a URL of the form:
[email protected]:private-developers/...
62
votes
2
answers
36k
views
Access main package from other package
I want to access the main package from another package, but this is impossible because the main file isn't in a directory. I already tried putting the main file in a directory, but when I try to ...
56
votes
5
answers
39k
views
Is there any way to access private fields of a struct from another package?
I have a struct in one package that has private fields:
package foo
type Foo struct {
x int
y *Foo
}
And another package (for example, a white-box testing package) needs access to them:
...
52
votes
3
answers
84k
views
Golang, importing packages from Github requests me to remember the Github URL?
I'm very new to Golang. I see that in Golang you can import packages directly from Github like:
import "github.com/MakeNowJust/heredoc"
Does that mean I have to remember this URL in order to use ...
49
votes
10
answers
17k
views
What's a good practice regarding sharing the GOPATH?
I'm just getting into learning Go, and reading through existing code to learn "how others are doing it". In doing so, the use of a go "workspace", especially as it relates to a project's dependencies, ...
49
votes
5
answers
44k
views
Load package dynamically
Is it possible to load a specific package during runtime?
I want to have a kind of plugins where each one has the same functions than the others but with different behaviour, and depending on the ...
46
votes
4
answers
19k
views
Relationship between a package statement and the directory of a .go file
See this experiment.
~/go/src$ tree -F
.
├── 1-foodir/
│ └── 2-foofile.go
└── demo.go
1 directory, 2 files
~/go/src$ cat demo.go
package main
import (
"fmt"
"1-foodir"
)
func main() {
...
29
votes
1
answer
14k
views
Go (golang) Package consisting of several folders
Is it possible in Go (golang) to have a package consisting of several .go files from different folders?
I am trying to make a subfolder inside of the main folder, and compiler says that it cannot ...
26
votes
2
answers
77k
views
Completely remove a package installed with "go get"?
I'm using Go 1.13.1, latest as of today.
I'm trying to completely remove a package that I installed with go get from GitHub. The go clean -i <PACKAGE_NAME> didn't seem to work, since there are ...
26
votes
4
answers
18k
views
Is there an efficient way to share structure between golang packages?
I have a short program in Go with the following files part of it.
Program directory structure:
myprogram/
main.go
server.go
routines.go
structs.go
These different files contain ...
26
votes
4
answers
48k
views
Build and reference my own local package in Go
I'm playing with Google Go and I'm having fun (!), but I'm having some problems with package subsystem.
I'm running Go 1.0.1 on Mac OS X Lion. I've build also various single file programs without ...
22
votes
2
answers
18k
views
How to run go test on all test files in my project except for vendor packages
My project folder contains:
Makefile README.md component/ driver/ service/ vendor/ worker/
I'd like to run go test on all test files, e.g. foobar_test.go files except for the test files in the ...
21
votes
2
answers
11k
views
How does one use a variable name with the same name as a package in Go?
A common variable name for files or directories is "path". Unfortunately that is also the name of a package in Go. Besides, changing path as a argument name in DoIt, how do I get this code to ...
20
votes
3
answers
35k
views
Installing packages in a local directory
What is the best practice to install packages (those with go get...) in a local directory?
Example: I'd like to try out the Revel web framework, but I don't want to clutter my go installation at /usr/...
19
votes
2
answers
21k
views
how to version packages in golang?
I've read a whole bunch of articles and SO questions on importing 3rd party go packages which all seems straight forward, but what I don't understand is that none that I have read make any references ...
19
votes
1
answer
21k
views
Go and namespaces: is it possible to achieve something similar to Python?
I wonder if there is a way to use namespaces in the Go language in a similar way as Python does.
In Python if I have the following files containing functions:
/a.py
def foo():
/b.py
def bar()...
18
votes
2
answers
5k
views
Why are Some golang.org Packages Prefixed with an `x`
Why are some golang.org package names prefixed with an x? The specific package that piqued my interest was bycrypt
My main concern is the x means something like eXperimental, and I should use a ...
17
votes
5
answers
3k
views
How to package go project for homebrew
We're developing runscripts and try to support something like brew install runscripts.
It's written in golang and have some dependencies which required to go get. Now I have no idea to write the ...
16
votes
6
answers
15k
views
Parse a command line string into flags and arguments in Golang
I'm looking for a package that would take a string such as -v --format "some example" -i test and parse it into a slice of strings, handling quotes, spaces, etc. properly:
-v
--format
some example
-i
...
15
votes
2
answers
16k
views
Can't define receiver from another package in Go
I'm a beginner in Golang and can't understand some concepts in this language. I like it very much, but every examples on the web are very simple and not explain the correct way of developing.
I want ...
15
votes
3
answers
26k
views
Go package with multiple files, how to structure
Go noob, I cannot seem to figure out how to structure my project with packages. What I want is this:
I want to create a package, lets say its called Dart.
I have a file called dart.go with ...
15
votes
2
answers
22k
views
How to access variables in other package
How can I move my global variable to other package in Go?
like
package main
import "myapp/controllers"
var something string
func main(){
something = "some text"
}
14
votes
2
answers
14k
views
Golang Gin-Gonic Split Routes into Multiple Files
I am looking to split my routes.go into multiple files so that each group is in its own package. Can someone point me to an example of some code where someone has done this with Gin?
i.e.
package ...
14
votes
6
answers
12k
views
Why can't IntelliJ IDEA import local packages in Go project?
I'm using Idea plugin for Go to work with my project.
The structure of my project is the following:
controller, entity, model, repository etc - are local packages (where one can use another).
...
14
votes
2
answers
14k
views
How do packages with multiple files work in golang?
This repo has 3 go files all begin with "package lumber".
To use this package, I'm supposed to put this in my GOROOT and simply
import lumber
in my program. How do variables and types in this ...
14
votes
3
answers
22k
views
Why does the "go run" command fail to find a second file in the main package?
I am experiencing an issue where go run main.go produces the error:
# command-line-arguments
./main.go:9: undefined: test
However the commands go build && ./goruntest compile and run the ...
12
votes
2
answers
13k
views
How does modular code work in Go?
Not having come from a C/compiled languages background, I'm finding it hard to get to grips with using Go's packages mechanism to create modular code.
In Python, to import a module and get access to ...
12
votes
5
answers
8k
views
Does golang have a central repository for the downloaded third-party packages?
I'm new to Golang. As I understand, when you want to create a new Go project, we just need to create a directory. Then we point the environment variable GOPATH to this directory. Inside this directory,...
11
votes
2
answers
2k
views
Disambiguate package name from local var in Go
Is there a good way to disambiguate between a package name and a local variable? I'd rather not refactor the import name or variable name if I don't have to. Take for example...
import "path"
func ...
11
votes
1
answer
6k
views
Importing local changes of a package without pushing code in Golang
I' am learning Golang now-a-days and a total newbie. I have a question regarding packages.
Consider the following scenario:
Imagine I have a package github.com/ilatif/A in which I' am importing ...
11
votes
1
answer
22k
views
Go library package names
I have some questions on package naming for external Go libraries.
I am interested if using generic names like "text" is considered a good practice? Having in mind that I cannot declare a "nested ...
10
votes
4
answers
7k
views
cannot refer to unexported name m.β
Have a look at these two simple packages:
package m
const β = 1
package main
import ("m";"fmt")
func main() {
fmt.Println(m.β)
}
I get this error when I try to compile them:
$ GOPATH=`pwd` go ...
10
votes
1
answer
3k
views
Visibility in sub-packages
How's the visibility of members from a sub-package to its root package?
This is what I mean:
foo // the "root" package
foo/utils // a sub-package
foo/tools // another sub-package
Can ...
10
votes
1
answer
11k
views
Implementing interface from different package golang
I'm having some issues trying to implement an interface, defined in a different package in golang. I have made a minimal recreation of the problem below
Interface:
package interfaces
type ...
10
votes
1
answer
4k
views
How do packages work in golang
I was trying to understand how packages work in go better in terms of what golang actually enforces rather than what is usually done or considered good practice (we can talk about good practice later ...
10
votes
1
answer
15k
views
go mod tidy "all" matched no packages [duplicate]
I've created a go module using this:
go mod init rtws
vim main.go # pasted a bunch of code from an example
go mod tidy
Both of these files import "github.com/gorilla/websocket", so when I ...
10
votes
1
answer
9k
views
Importing local library and files in an application
I'm new to Go (but not at programming), I love the language but I have a bit of trouble fully understanding the way I'm supposed to make internal libraries in an application through packages. For ...