Go Lang Cheatsheet

Syed Muhammad Ali Raza - May 19 - - Dev Community

Table of Contents

  1. Getting Started
  2. Basic Syntax
  3. Data Types
  4. Control Structures
  5. Functions
  6. Structs and Interfaces
  7. Concurrency

1. Installation

To install Go, download it from the official website and follow the installation instructions for your operating system.

Create a new Go file with the .go extension, for example, main.go.

2. Basic Syntax

Hello, World!

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

Start

  • The main package is required for executable commands.
  • The main function is the entry point of a Go program.
  • fmt is a standard library package for formatted I/O.

3. Data Types

Variable Declaration

var name string = "Go"
var age int = 10
Enter fullscreen mode Exit fullscreen mode

Short Variable Declaration

name := "Go"
age := 10
Enter fullscreen mode Exit fullscreen mode

Constants

const Pi = 3.14
Enter fullscreen mode Exit fullscreen mode

Basic Types

  • Integers: int, int8, int16, int32, int64
  • Unsigned Integers: uint, uint8, uint16, uint32, uint64
  • Floats: float32, float64
  • Complex: complex64, complex128
  • Others: byte (alias for uint8), rune (alias for int32), bool, string

4. Control Structures

If-Else

if age > 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}
Enter fullscreen mode Exit fullscreen mode

For Loop

for i := 0; i < 5; i++ {
    fmt.Println(i)
}
Enter fullscreen mode Exit fullscreen mode

Switch

switch day {
case "Monday":
    fmt.Println("Start of the week")
case "Friday":
    fmt.Println("End of the week")
default:
    fmt.Println("Midweek")
}
Enter fullscreen mode Exit fullscreen mode

5. Functions

Basic Function

func add(a int, b int) int {
    return a + b
}
Enter fullscreen mode Exit fullscreen mode

Multiple Return Values

func swap(x, y string) (string, string) {
    return y, x
}
Enter fullscreen mode Exit fullscreen mode

Named Return Values

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return
}
Enter fullscreen mode Exit fullscreen mode

6. Structs and Interfaces

Structs

type Person struct {
    Name string
    Age  int
}

p := Person{Name: "Alice", Age: 30}
Enter fullscreen mode Exit fullscreen mode

Methods

func (p Person) greet() string {
    return "Hello, " + p.Name
}
Enter fullscreen mode Exit fullscreen mode

Interfaces

type Animal interface {
    Speak() string
}

type Dog struct{}

func (d Dog) Speak() string {
    return "Woof"
}
Enter fullscreen mode Exit fullscreen mode

7. Concurrency

Goroutines

go func() {
    fmt.Println("In a goroutine")
}()
Enter fullscreen mode Exit fullscreen mode

Channels

messages := make(chan string)

go func() {
    messages <- "ping"
}()

msg := <-messages
fmt.Println(msg)
Enter fullscreen mode Exit fullscreen mode

Select

select {
case msg1 := <-chan1:
    fmt.Println("Received", msg1)
case msg2 := <-chan2:
    fmt.Println("Received", msg2)
default:
    fmt.Println("No message received")
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

This cheat sheet provides a quick overview of Go's basic syntax and features. Go's simplicity and efficiency make it an excellent choice for various applications, from web servers to distributed systems. For more detailed information, refer to the official Go documentation and Do Contact me on LinkedIn Syed Muhammad Ali Raza

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player