Why don't I have to do the Go equivalent of 'npm install' when I clone the repository down?

Viewed 32

Beginner to Go but I am used to running npm install once I clone a repo to my local machine. But it does not seem like you do this in Go. Could someone help me understand? Thanks!

1 Answers

Javascript is interpreted. The various libraries used by your code must be available at runtime, and will be loaded in dynamically when your javascript program executes.

Go is compiled. Libraries are typically resolved at compile time, and the resultant binary has been built from the required components of libraries and your source code.

I say typically because if you have CGo enabled, some share object files may be required to execute your program at runtime. In this case, the shared object files will have to be installed somehow on a system before it can run your code. Still, resolving links to shared object files is an old and mature technology, one which npm could not directly rely on, so it needed npm install for the interpreted equivalent.

If CGo is disabled, a go binary can execute on the architecture and OS it was installed on. All dependencies are relevant at compile time only. Go modules take care of including the required libraries before compiling.

Welcome to Go. Keep an open mind as you learn; static typing and lack of inheritance makes Go very different from Javascript, but therein lies many of its features as well. So embrace Go for what it is, and I'm sure you'll have fun and learn a lot.

Related