Live data from Hacker News

Using Makefile(s) for Go

danishpraka.sh

101–104 of 104 posts

Re: Using Makefile(s) for Go

#101
post #49
post #24

Earlier quoted context omitted.

Make is available everywhere that matters, and is a simple declarative way to encompass build actions. What are the alternatives? Bash? Not declarative, and requires lots more code. Some go rewrite of Make? Not universal, possibly not maintained in the future. Rake? Ugh, Ruby. I strongly believe that make is the least worst way to build go projects, but please change my mind by suggesting some alternatives, not by co…

This is patently untrue. Make is not available by default on Windows, which - whether you like it or not - matters as a platform for a large number of developers. _Fortunately_ some CI images (certainly GitHub Actions and Azure DevOps) install GNU Make on their windows images by default, but it absolutely cannot be assumed for average developers.

Well, by default, nothing is available on windows.

Re: Using Makefile(s) for Go

#102
Nice article. I use makefiles a lot, mostly for all projects that I use, both for frontend and backend, mainly to have the same commands independently of framework/platform that im using. For me its helpful to just run `make` both to build and run a go project and a react project.

Another thing you can add is:

.DEFAULT_GOAL := start

start: fmt swag vet build run

Helps define you default command soyou just need to run `make` and will run all inside of `start`

Since most of us use `.env` files for enviroment files, you can use something like:

# this allows to import into this file all current system envs

include .env

export

And it will inject all of .env file into the current running `process`

Also have some other shortcuts (variables):

GOCMD=go

GOBUILD=$(GOCMD) build

GOCLEAN=$(GOCMD) clean

GOTEST=$(GOCMD) test

GOFMT=gofmt -w

GOGET=$(GOCMD) mod download

GOVER=$(COCMD) vet

GOFILES=$(shell find . -name "*.go" -type f)

BINARY_NAME=my-cool-project

BINARY_UNIX=$(BINARY_NAME)_prod

Re: Using Makefile(s) for Go

#103
post #11

This... isn't even using the `make` part of Makefiles at all. If you look at the final example, every [1] rule is marked as `.PHONY`. `make` bundles 2 capabilities: a dependency graph and an out-of-date check to rebuild files. This demonstration uses neither. The author would be better served with a shell script and a `case` block. The advantages: - Functions! The `check-environment` rule is really a function call in…

I'm more confused as to why use .PHONY in so many places. Golang builds from .go files whos modification times are changed when written to, same as .c and .cpp files, so make is able to know when the go compiler needs to be called, or not.

Re: Using Makefile(s) for Go

#104
post #11

This... isn't even using the `make` part of Makefiles at all. If you look at the final example, every [1] rule is marked as `.PHONY`. `make` bundles 2 capabilities: a dependency graph and an out-of-date check to rebuild files. This demonstration uses neither. The author would be better served with a shell script and a `case` block. The advantages: - Functions! The `check-environment` rule is really a function call in…

I'm more confused as to why use .PHONY in so many places. Golang builds from .go files whos modification times are changed when written to, same as .c and .cpp files, so make is able to know when the go compiler needs to be called, or not.

Make absolutely does not understand all the scenarios for rebuild, and go build has much more complicated logic than that. For one, it's content-based not mtime-based. For another, a command needs rebuild if any of its dependencies change, not just its own source file. Make absolutely does not get that right for Go.
Post reply on HN