go tool can create a binary and place it to GOBIN if package contains a main sub package (or if package is a main). Is there a possibility to create a few (at least two) binaries with single go install package command? Meaning without using GNU make for this purposes.
Thank you.
It is definitely possible if all commands are under a common directory, using go install root/.... The trailing three dots tell the go command to do this for all packages under this directory. The same three-dots notation works for go get or go build and probably all go commands.
An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns. As a special case, x/... matches x as well as x's subdirectories. For example, net/... expands to net and packages in its subdirectories.
http://golang.org/cmd/go/
AFAIK this is not possible. The usual convential is that you put your binaries into packages that have cmd as their last path element. People can then install all the binaries like this:
go get code.google.com/p/codesearch/cmd/{cindex,csearch,cgrep}
Related
I just ran the command
echo $PATH
The output was:
/opt/anaconda3/condabin:/usr/local/opt/python/libexec/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
Why is it so long? Shouldn't it look more like
/usr/local/bin?
It is quite normal to have a PATH variable that is long. Yours contain some duplicate entries but this is not a real problem.
If you execute a command without specifing a path the shell searches in each path specified in the PATH variable to find it. In your example it search in /opt/anaconda3/condabin and after that in /usr/local/opt/python/libexec/bin and so on.
It is normal to have multiple places for executables and thus multiple entries in the PATH variable separated with colon. Often third party software like Anaconda in your example adds it own folder to the PATH variable.
Have a look at /etc/paths, /etc/paths.d and your shell profile where those path might be added.
And from a security standpoint your PATHS could be risky if /opt/anaconda3/condabin or /usr/local/opt/python/libexec/bin is writeable by the user/others as anybody can place malicious executable there masking the system supplied ones in /bin or /usr/bin.
If not absolutely neccessary third party pathes should be added to the end as they normally shouldn't replace existing binaries.
I am currently looking at this project (Mattermost) that has a certain line in the makefile that I'm confused about:
$(GO) run $(GOFLAGS) $(GO_LINKER_FLAGS) ./cmd/platform/*.go --disableconfigwatch &
What is the meaning of ./cmd/platform/*.go? What specific files are executed? The program executes correctly when I type it in the terminal.
I am trying to enter the command line arguments in an IDE but I need a specific entry file.....
Wikipedia
glob (programming)
In computer programming, in particular in a Unix-like environment,
glob patterns specify sets of filenames with wildcard characters. For
example, the Unix command mv *.txt textfiles/ moves (mv) all files
with names ending in .txt from the current directory to the directory
textfiles. Here, * is a wildcard standing for "any string of
characters" and *.txt is a glob pattern. The other common wildcard is
the question mark (?), which stands for one character.
Clearly, ./cmd/platform/*.go, starting in the current directory, looks in the cmd/platform directory for files matching the wildcard *.go.
The ls ./cmd/platform/*.go command will list the files on Linux.
So, the go run ./cmd/platform/*.go command compiles and runs these Go (*.go) source files. See Command go documentation: Compile and run Go program.
I usually see ./... in golang
for example go test ./...or go fmt ./...
only know the meaning of one or two dots
It means perform the action on all packages under a directory. So for example go test ./... runs go test on the current dir + all subdirectories.
The Go tool documentation is here:
https://golang.org/doc/cmd
./... means a recursive action ( ... ) from your current directory ( ./ )
An import path beginning with ./ or ../ is called a relative path. The toolchain supports relative import paths as a shortcut in two ways.
First, a relative path can be used as a shorthand on the command line. If you are working in the directory containing the code imported as "unicode" and want to run the tests for "unicode/utf8", you can type "go test ./utf8" instead of needing to specify the full path. Similarly, in the reverse situation, "go test .." will test "unicode" from the "unicode/utf8" directory. Relative patterns are also allowed, like "go test ./..." to test all subdirectories. See 'go help packages' for details on the pattern syntax.
Second, if you are compiling a Go program not in a work space, you can use a relative path in an import statement in that program to refer to nearby code also not in a work space. This makes it easy to experiment with small multipackage programs outside of the usual work spaces, but such programs cannot be installed with "go install" (there is no work space in which to install them), so they are rebuilt from scratch each time they are built. To avoid ambiguity, Go programs cannot use relative import paths within a work space.
Run go help importpath or see the docs here https://pkg.go.dev/cmd/go#hdr-Relative_import_paths
This question already has answers here:
What do three dots "./..." mean in Go command line invocations?
(2 answers)
Closed 6 years ago.
I am having trouble understanding what the following command does.
go list ./...
When I look at the official documentation https://golang.org/cmd/go/#hdr-List_packages, it is not clear to me what the ./... argument is telling the command.
go list requires an import path for a package and can give you some listing info for the packages matched this way (you may be interested in its -f flag).
./... is a wildcard that matches the current folder and all of its subfolders (execute go help packages to read more about this somewhat hidden feature), and that can be used with all the built-in Go commands.
So go list ./... can list all the Go packages in the current folder and its sub-folders - you may want to call it from GOPATH for example.
go list ./...
Here ./ tells to start from the current folder, ... tells to go down recursively.
go list ...
In any folder lists all the packages, including packages of the standard library first followed by external libraries in your go workspace.
When you have multiple .go files in a main package, i need to list them all out when doing a go run.
So when i have main.go, a.go, b.go and they all belong to the main package, i need to type go run main.go a.go b.go in order to use functions and structs inside the other 2 go files. The go build command however, is smart enough to link all files together automatically.
Have i misunderstood something about Go, or is this normal (listing out all the files in the main package when doing a go run)?
The short answer is: you need to list them all out. You can do this with shell tricks, if you're desperate. I usually just write a shell script to go build and then run the resulting executable, after it gets too annoying.
# this works unless you have _test.go files
go run *.go
# if you really want to... enable extended glob
shopt -s extglob
# so you can do something like this (matches all .go except *_test.go files)
go run !(*_test).go
Check out this thread: https://groups.google.com/forum/#!topic/golang-nuts/EOi0VAQNA4c
If your source is in $GOPATH/src
as in
$GOPATH/src/somedir/main.go
$GOPATH/src/somedir/a.go
$GOPATH/src/somedir/b.go
doing "go install somedir" will compile and install the somedir binary in to $GOPATH/bin
no need to list out the files individually
"go install" finds all the files on it's own.
My common work cycle with go is (from my bash prompt):
$ clear; go install myproj && $GOPATH/bin/myproj
which, clears the screen, builds and installs "myproj" and then runs it
so, after I change code, I just hit Ctrl-C, Up arrow, and enter.
Entire cycle time is ~1 second (for small to start stuff)