- Go is a systems-level programming language for large, distributed systems and highly-scalable network servers.
- Go was created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thomson.
- Go combines the safety and performance of statically-typed compiled language (e.g. C++, C#, Java) with expressiveness and convenience of dynamically-types interpreted language (e.g. Perl, Python, JavaScript).
- Go can be used for server-side web development, network-based applications, cloud-native development.
Installation of Go on Windows
- Visit https://go.dev/and download the latest version.
- Installation is a simple wizard-based process.
- You may choose the installation folder.
- After successful installation, you can check the Go installation by opening a command prompt and type go -version
- The Go installer creates a GOPATH environment variable and by default set it to C:\Users\USER_NAME\go.
- GOPATH points to Go Workspaces.
- Go workspace is hierarchy of folders to manage source files, compiled binaries and cached objects for faster compilation.
- There are three directories inside the workspace.
- bin: location of compiled executable programs built by Go
- pkg: location of compiled package code
- src: location of Go source code
- For practice purpose lets create a workspace for Go applications e.g. Practice.
- To tell Go about our new workspace set GOPATH to Practice folder. Right click My Computer and select Properties. On properties screen select Advanced System Settings.
- On System properties window click Environment Variables.
- In next dialog select GOPATH and click Edit button.
- Replace variable value with path of your workspace (E:\Edu\Go\Practice) and click Ok. Close variables dialog and then System properties dialog. To verify that new value is assigned to GOPATH; run the following command from Command prompt.
Installation of Visual Studio Code on Windows
- Visual Studio Code is an editor which you can use for writing Go programs.
- To download Visual Studio Code visit the website https://code.visualstudio.com/Download
- Once the download is complete, run the exe to install Visual Studio Code.
- On the Select Addition Tasks dialog check all the available options and click next.
- On next dialog click install button and wait for the installation to finish.
- Launch Visual Studio Code and bring up the Extensions view by clicking on the Extensions icon in the Activity Bar or use shortcut (Ctrl+Shift+X).
- Search and install the Go extension.
- In Visual Studio Code, open Command Palette's from Help > Show All Commands or use shortcut (Ctrl+Shift+P) and update Go tools by typing Go: Install/Update tools and checking all the tools shown. Wait for the Go tools to finish updating. VS Code Go will install the tools to your GOPATH/bin by default.
Hello World Go Program
- To open Practice folder (workspace created earlier) in Visual Studio click Open Folder from File menu and select Practice directory.
- Right click Practice folder and create src folder. In src folder create HelloWorld directory for our hello world app.
- In HelloWorld directory create a main.go file and type a simple program to print hello world.
The package keyword declares the package with the name βmainβ. Package is the collection of source files in the same directory that are compiled together. There are two types of Go programs; executables and libraries. An executable package requires to be created with package name main and main function declaration.
The import statement includes a package into our program. The fmt package (shorthand for format) implements formatting for input and output.
All functions start with func keyword. The main function is special because it gets called when program is executed. The Println function of fmt package prints the string to the standard output (output of terminal).
- At very first line we are faced with error βgo.mod file not found in current directory or any parent directory; see 'go help modules'β.
- Go.mod file specifies a module name, dependencies, and minimal versions. The go mod init command initializes and writes a new go.mod file in the current directory and also creates pkg folder in our workspace.
- There are three ways to run Go program.
- go run command compiles the code into a binary file, places it in a temporary directory, executes this binary file, and deletes it after your program finishes.
- go build command compiles the code into a binary file and places it in the current directory without executing it.
- go install command compiles the code into a binary file, creates GOPATH /bin directory and places exe in it without executing it.
Top comments (0)