-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d890c3
commit 62de8e6
Showing
13 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
# Go (Golang) Basics | ||
|
||
## Table of Contents | ||
1. [Introduction](#introduction) | ||
2. [Installation](#installation) | ||
- [Windows](#windows) | ||
- [macOS](#macos) | ||
- [Linux](#linux) | ||
3. [Setting Up Your Environment](#setting-up-your-environment) | ||
4. [Basic Concepts](#basic-concepts) | ||
- [Hello World](#hello-world) | ||
- [Variables](#variables) | ||
- [Functions](#functions) | ||
- [Control Structures](#control-structures) | ||
- [Data Structures](#data-structures) | ||
5. [Running Your Go Code](#running-your-go-code) | ||
6. [Further Reading](#further-reading) | ||
|
||
--- | ||
|
||
## Introduction | ||
|
||
Go, often referred to as Golang, is an open-source programming language developed by Google. It is designed to be simple, efficient, and reliable, with strong support for concurrent programming. | ||
|
||
## Installation | ||
|
||
### Windows | ||
|
||
1. **Download Go Installer** | ||
- Go to the [Go downloads page](https://golang.org/dl/). | ||
- Download the Windows installer (`.msi` file) for your system architecture (e.g., `go1.20.4.windows-amd64.msi`). | ||
|
||
2. **Run the Installer** | ||
- Double-click the downloaded `.msi` file and follow the installation prompts. | ||
- The default installation path is `C:\Go`. | ||
|
||
3. **Verify Installation** | ||
- Open Command Prompt and type `go version`. You should see the Go version information. | ||
|
||
### macOS | ||
|
||
1. **Install Using Homebrew** | ||
- Open Terminal and run the following command to install Homebrew if you haven’t already: | ||
```sh | ||
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | ||
``` | ||
- Install Go with Homebrew: | ||
```sh | ||
brew install go | ||
``` | ||
|
||
2. **Verify Installation** | ||
- In Terminal, type `go version` to check if Go was installed correctly. | ||
|
||
3. **Alternative Installation** | ||
- Download the macOS binary (`.pkg` file) from the [Go downloads page](https://golang.org/dl/). | ||
- Open the downloaded `.pkg` file and follow the installation instructions. | ||
|
||
### Linux | ||
|
||
1. **Download and Extract** | ||
- Open Terminal and download the latest Go tarball from the [Go downloads page](https://golang.org/dl/): | ||
```sh | ||
wget https://golang.org/dl/go1.20.4.linux-amd64.tar.gz | ||
``` | ||
- Extract the tarball: | ||
```sh | ||
sudo tar -C /usr/local -xzf go1.20.4.linux-amd64.tar.gz | ||
``` | ||
|
||
2. **Set Up Environment Variables** | ||
- Add Go binary to your PATH. Open or create `~/.profile` or `~/.bashrc` and add: | ||
```sh | ||
export PATH=$PATH:/usr/local/go/bin | ||
``` | ||
- Reload the profile: | ||
```sh | ||
source ~/.profile | ||
``` | ||
|
||
3. **Verify Installation** | ||
- Type `go version` in Terminal to verify the installation. | ||
|
||
## Setting Up Your Environment | ||
|
||
1. **Create a Workspace** | ||
- Create a directory for your Go projects: | ||
```sh | ||
mkdir -p ~/go/src | ||
``` | ||
- Set the `GOPATH` environment variable to this directory. Add the following to `~/.profile` or `~/.bashrc`: | ||
```sh | ||
export GOPATH=$HOME/go | ||
export PATH=$PATH:$GOPATH/bin | ||
``` | ||
- Reload the profile: | ||
```sh | ||
source ~/.profile | ||
``` | ||
|
||
2. **Create a Go Module** | ||
- Initialize a new Go module in your project directory: | ||
```sh | ||
mkdir myproject | ||
cd myproject | ||
go mod init myproject | ||
``` | ||
|
||
## Basic Concepts | ||
|
||
### Hello World | ||
|
||
1. **Create a New File** | ||
- Create a file named `main.go` in your project directory with the following content: | ||
```go | ||
package main | ||
import "fmt" | ||
func main() { | ||
fmt.Println("Hello, World!") | ||
} | ||
``` | ||
|
||
2. **Run the Program** | ||
- Execute the program by running: | ||
```sh | ||
go run main.go | ||
``` | ||
|
||
### Variables | ||
|
||
1. **Declaring Variables** | ||
- Variables can be declared using the `var` keyword or using shorthand syntax: | ||
```go | ||
var name string = "John" | ||
age := 30 | ||
``` | ||
|
||
2. **Constant Declaration** | ||
- Constants are declared using the `const` keyword: | ||
```go | ||
const Pi = 3.14 | ||
``` | ||
|
||
### Functions | ||
|
||
1. **Defining a Function** | ||
- Functions are defined with the `func` keyword: | ||
```go | ||
func greet(name string) string { | ||
return "Hello, " + name | ||
} | ||
``` | ||
|
||
2. **Calling a Function** | ||
- Call the function in `main`: | ||
```go | ||
func main() { | ||
message := greet("Alice") | ||
fmt.Println(message) | ||
} | ||
``` | ||
|
||
### Control Structures | ||
|
||
1. **If Statements** | ||
- Example of an `if` statement: | ||
```go | ||
if age > 18 { | ||
fmt.Println("Adult") | ||
} else { | ||
fmt.Println("Minor") | ||
} | ||
``` | ||
2. **For Loops** | ||
- Example of a `for` loop: | ||
```go | ||
for i := 0; i < 5; i++ { | ||
fmt.Println(i) | ||
} | ||
``` | ||
### Data Structures | ||
1. **Arrays** | ||
- Example of an array: | ||
```go | ||
var numbers [5]int | ||
numbers[0] = 1 | ||
``` | ||
2. **Slices** | ||
- Slices are more flexible than arrays: | ||
```go | ||
fruits := []string{"apple", "banana", "cherry"} | ||
``` | ||
3. **Maps** | ||
- Example of a map: | ||
```go | ||
ages := make(map[string]int) | ||
ages["Alice"] = 30 | ||
``` | ||
4. **Structs** | ||
- Example of a struct: | ||
```go | ||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
func main() { | ||
p := Person{Name: "John", Age: 25} | ||
fmt.Println(p) | ||
} | ||
``` | ||
## Running Your Go Code | ||
1. **Run a Go File** | ||
- Use the `go run` command followed by the filename: | ||
```sh | ||
go run main.go | ||
``` | ||
2. **Build an Executable** | ||
- Build the Go file into an executable: | ||
```sh | ||
go build main.go | ||
``` | ||
- Run the generated executable: | ||
```sh | ||
./main | ||
``` | ||
## Further Reading | ||
- [The Go Programming Language Documentation](https://golang.org/doc/) | ||
- [Effective Go](https://golang.org/doc/effective_go.html) | ||
- [A Tour of Go](https://tour.golang.org/welcome/1) | ||
- [Go by Example](https://gobyexample.com/) | ||
--- | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module hello | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("Hello, World!") | ||
|
||
j := 15 | ||
fmt.Println(j) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
p:= 64 | ||
q:= 32 | ||
|
||
result:= p+q | ||
fmt.Println("Sum of p and q: ", result) | ||
result= p-q | ||
fmt.Println("Difference of p and q: ", result) | ||
result= p*q | ||
fmt.Println("Product of p and q: ", result) | ||
result= p/q | ||
fmt.Println("Quotient of p and q: ", result) | ||
result= p%q | ||
fmt.Println("Remainder of p and q: ", result) | ||
// result= p<<q | ||
// fmt.Println("Left shift of p by q: ", result) | ||
// result= p>>q | ||
// fmt.Println("Right shift of p by q: ", result) | ||
// result= ^p | ||
// fmt.Println("Bitwise NOT of p: ", result) | ||
// result= p&q | ||
// fmt.Println("Bitwise AND of p and q: ", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
//assignment operators | ||
|
||
var a, b int = 10, 5 | ||
var c, d string = "Tech with Bansikah", "Tech with Bansikah" | ||
|
||
a += b | ||
fmt.Println("a += b: ", a) | ||
|
||
a -= b | ||
fmt.Println("a -= b: ", a) | ||
|
||
a *= b | ||
fmt.Println("a *= b: ", a) | ||
|
||
a /= b | ||
fmt.Println("a /= b: ", a) | ||
|
||
a %= b | ||
fmt.Println("a %= b: ", a) | ||
|
||
c += d | ||
fmt.Println("c += d: ", c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
import "fmt" | ||
|
||
func main() { | ||
//bitwise operators | ||
var a, b int = 10, 5 | ||
fmt.Println("Bitwise AND:", a & b) | ||
fmt.Println("Bitwise OR:", a | b) | ||
fmt.Println("Bitwise XOR:", a ^ b) | ||
fmt.Println("Bitwise NOT:", ^a) | ||
fmt.Println("Left shift:", a << 2) | ||
fmt.Println("Right shift:", a >> 2) | ||
fmt.Println("Bitwise AND with 1111111111111111:", a & 0xFFFF) | ||
fmt.Println("Bitwise OR with 0000000000000001:", a | 1) | ||
fmt.Println("Bitwise XOR with 0000000000000001:", a ^ 1) | ||
// bitwise operators end here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
//comparison operators | ||
var a, b int = 10, 5 | ||
var c, d string = "Tech with Bansikah", "Tech with Bansikah" | ||
|
||
result := a == b | ||
result1 := a != b | ||
result2 := a < b | ||
result3 := a <= b | ||
result4 := a > b | ||
result5 := a >= b | ||
result6 := c == d | ||
result7 := c != d | ||
|
||
fmt.Println(result) | ||
fmt.Println(result1) | ||
fmt.Println(result2) | ||
fmt.Println(result3) | ||
fmt.Println(result4) | ||
fmt.Println(result5) | ||
fmt.Println(result6) | ||
fmt.Println(result7) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
//logical operators | ||
var a, b bool = true, false | ||
fmt.Println("Logical AND:", a && b) | ||
fmt.Println("Logical OR:", a || b) | ||
fmt.Println("Logical NOT:", !a) | ||
fmt.Println("Logical XOR:", a != b) | ||
} |
Oops, something went wrong.