29

My folder structure is correct, i can both run go install from inside the package folder and from anywhere in the system, adding the package (folder) name after install.

For example, my workspace is the following:

Go\
  bin\
  pkg\
  src\
    name\
      file.go

then, if i run

cd %GOPATH%\src\name
go install

or

go install name

no errors are generated and my workspace becomes the following

Go\
  bin\
  pkg\
    windows_amd64\ <-- new!
      name.a       <-- new!
  src\
    name\
      file.go

Package files are correctly created, but bin files aren't.

My go env is the following:

C:\Users\...>go env
set GOARCH=amd64
set GOBIN=C:\Users\myname\Documents\Go\bin
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\myname\Documents\Go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

Why is this the case? Am i missing something, doing something wrong? I want bin files to be created along with package files.

castan
  • 383
  • 1
  • 4
  • 11

3 Answers3

58

One reason could be the file.go isn't in package main.
See for instance "Your first program"

If it was, that would generate a executable in bin.

The article "How does the go build command work ?" does mention:

A Go command is a package whose name is main.
Main packages, or commands, are compiled just like other packages, but then undergo several additional steps to be linked into final executable.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • is there any way to get some kind of debug output from go install? so frustrating. cant it show me what its trying to do at least instead of just giving no output – red888 Jan 04 '21 at 03:07
  • 2
    @red888 `go install` (https://golang.org/pkg/cmd/go/#hdr-Compile_and_install_packages_and_dependencies) accepts build flags (https://golang.org/pkg/cmd/go/#hdr-Compile_packages_and_dependencies), so you can at least pass `-v` to print the names of packages as they are compiled, `-x` to print the commands, and `-work`, to print the name of the temporary work directory and do not delete it when exiting, for further inspection. – VonC Jan 04 '21 at 10:59
4

I came to this question with a different problem so hopefully this helps someone. This isn't answering this question specifically, but it does answer for me at least why no binary file was being made.

Let's say your directory structure looks like this:

.go/
  bin/
  pkg/
  src/
    github.com/
      banool/
        myapp/
          file.go
          server.go
          cmd/
            myapp/
              main.go

I had to run these two commands:

go build github.com/banool/myapp/...
go build github.com/banool/myapp/cmd/...

After this my binary appeared under myapp/.

Obviously this is a very different situation to the answer above, but I came to this question with such a situation, where I had run go get to get some Go source and I didn't know how to build it. Hopefully this helps people stumbling across this question with that problem :)

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
1

Well, as a first program, I used first line as package hello

Then it was creating "hello.a" under pkg folder. Moment I changed it to "package main", the executable was generated

Sanjay Chopra
  • 71
  • 1
  • 2