How to Download and install go in your device
Downloading Go
- Go to the official website of Go i.e. https://go.dev/
- Click download
- Select your Operating System and download the latest version
Installing Go on Different Operating Systems
- Windows
- Open the MSI file you downloaded and follow the prompts to install Go. By default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected at the command prompt.
- Verify that you’ve installed Go.
- In Windows, click the Start menu.
- In the menu’s search box, type cmd, then press the Enter key.
- In the Command Prompt window that appears, type the following command:
1 | $ go version |
Confirm that the command prints the installed version of Go.
- Linux
Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:
1 | $ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz |
(You may need to run the command as root or through sudo). Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.
Add /usr/local/go/bin to the PATH environment variable. You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
1 | $ export PATH=$PATH:/usr/local/go/bin |
Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.
Verify that you’ve installed Go by opening a command prompt and typing the following command:
1 | $ go version |
Confirm that the command prints the installed version of Go.
Mac
Open the package file you downloaded and follow the prompts to install Go.
The package installs the Go distribution to /usr/local/go. The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.
Verify that you’ve installed Go by opening a command prompt and typing the following command:
1 | $ go version |
Confirm that the command prints the installed version of Go.
Setting Up VS Code for Golang
Download and install vs code in your device:
- For Windows -> Microsoft Store
- For Linux -> Snap Store
- For Mac -> App Store
- Or else you can download it from its website: https://code.visualstudio.com/
Installing Go extention for VS code
- Go to Extensions section in vs code
- Search: “Go”
- install the 1st extension named “Go” by go.dev
Getting Started with GO
Creating Path for GO:
- Make a new folder anywhere in your directory and name it “GO_LANG”.
- Drag & Drop that folder in your VS code
- Create another new folder in that that folder and name it “ helloworld “.
- Create a file under that “helloworld” folder and name it “ main.go .
How to run your Go code in vs code??
- Right Click on the folder which you are using.
- Choose “Open integrated terminal”
- Go to the Terminal and Type: “go run <filename.go>” and press enter.
Go Mod
The go mod command is a fundamental part of the Go Modules system. It is used to create or initialize a Go module, which is a way to manage dependencies and versioning in your Go projects.
Follow the steps to initialize go mod:
Right click on the folder your are in and open integrated terminal.
Type “go mod init
“ and hit enter.
You have to mod every new folder you will make
Basic Structure of Go
1 | package main |
Printing “Hello World” in Go.
1 | package main |
The “fmt” package :- Stands for Format package. It offers a variety of functions to manipulate the format of input and output.
The “fmt” package contains many functions under it. Some of the most commonly used functions of the “fmt” package are :-
Sprintf - uses verbs like %s (string), %d(int), %f(float), %t(bool), etc. to interpolate values into a string and returns it as a single string.
Printf - formats the input in the same way as Sprintf before printing it
Println -simply prints the arguments it receives.
Data types in Go.
There are mainly 4 data types :- string, integer, float, boolean. They are further divided into several categories:-
Integers
- int = ranges from negative ∞ to positive ∞
- int8 = ranges from -128 to 127
- int16 = ranges from -32768 to 32767
- int32 = ranges from -2147483648 to 2147483647
- int64 = ranges -9223372036854775808 to 9223372036854775807
- uint8 = ranges from 0 to 255
- uint16 = ranges from 0 to 65535
- uint32 = ranges from 0 to 4294967295
- uint64 = ranges from 0 to 18446744073709551615
Float
- float32 = all 32-bit floating point numbers.
- float64 = all 64-bit floating point numbers.
Strings
- string = used for storing characters, letters, symbols, etc…
Boolean
- bool = Stores either true or false value.
Packages in Golang
Package - A package in Go is a collection of source files in the same directory that are compiled together. Packages are used to organize code and promote code reuse. Each Go program is made up of packages, and the program starts running in the main package.
Key Points about packages in Golang. :-
- Package Declaration :- Every Go source file starts with a package declaration. This defines the package to which the file belongs.
syntax:
1 | package package_name |
- Importing Packages:- To use functions, types, or variables from another packages, we need to import that package.
syntax:
1 | import "package_name" |
Variables
1 | package main |
- “var” keyword is used to declare a variable.
- “:=” is used for direct declaration of a variable.
- Default Values don’t store garbage values.
for eg. :- default string variables will return null value. default int/float variables will return 0.
Printing Strings with variables
1 | package main |
To Print the Data Type of a Given Value
1 | package main |
- “%T” is used for identifying the data type
Arithmetic Operations
1 | package main |
User Input
1 | package main |
Bufio or Buffered I/O package :- allows input and output with a buffer.
os package :- It allows you to access to operating system functionalities.
variable name.ReadString(“\n”) :- it reads the user input data in string form until the user enters the value given inside the brackets i.e. “\n” (new line) .