Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make -tags netgo the default for go?

Tags:

go

build

To compile my golang package so that it produces a statically linked executable, I must say:

go install -tags netgo

Additionally I now realise that on a system without gcc, I must:

go get -tags netgo github.com/mypackage/...
go test -tags netgo ./...

If you're typing this all the time it's no so nice. I have aliases set up so I can type less, but is there a "nicer" or "proper" way to set -tags netgo as some kind of default? Ideally as part of my package itself, so a stranger trying to work with my package doesn't miss the -tags netgo.

Edit: I also want the possibility to say:

go install -tags netgo -ldflags '-linkmode external -extldflags -static -w'

Ie. link to C code that can be statically compiled. So it working on a machine without GCC is less important to me than it producing a statically linked executable. I just wonder if there's a way to do the latter without saying -tags netgo all the time.

like image 661
sbs Avatar asked Sep 16 '16 14:09

sbs


People also ask

How do I run a go file?

To run a Go program (assuming you have installed Go on your system), you need to instruct the Go compiler to compile and run a program using go run command with the relative or absolute path of the Go program file.

How do you make HTTP request in Golang?

Get function is called, Go will make an HTTP request using the default HTTP client to the URL provided, then return either an http. Response or an error value if the request fails. If the request fails, it will print the error and then exit your program using os. Exit with an error code of 1 .

What is HTTP Go client?

Golang HTTP Performance. HTTP (hypertext transfer protocol) is a communication protocol that transfers data between client and server. HTTP requests are very essential to access resources from the same or remote server.

How do I install packages in go?

To install a package using go get follow the following steps: Step 1: Make sure to check whether the Golang is installed on your system by checking the version of Go. Step 2: Set the GOPATH by using the following command. Step 3: Now, set the PATH variable with the help of the following command.


1 Answers

You can disable cgo altogether by setting CGO_ENABLED=0 which will prevent the net package from linking to the host resolver.

like image 198
JimB Avatar answered Oct 18 '22 20:10

JimB