Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Basic Syntax in GoLang

Basic Syntax in GoLang

GoLang's syntax is designed to be simple, readable, and efficient. Below is an overview of its basic syntax:


1. Structure of a Go Program

package main // Defines the package name; 'main' is the entry point for the application import "fmt" // Import standard or third-party libraries func main() { // Entry point of the program fmt.Println("Hello, World!") // Print output to the console }


2. Comments

  • Single-line Comment:

    // This is a single-line comment

  • Multi-line Comment:

    /*This is a multi-line comment. It spans multiple lines. */


3. Variables

  • Declaring Variables:

    var x int // Declares a variable 'x' of type int var y = 10 // Type inference, 'y' is inferred to be int z := 20 // Short-hand declaration, works only inside functions

  • Multiple Variable Declaration:

    var a, b, c int = 1, 2, 3 var d, e = "Hello", true


4. Constants

  • Declared using const, and values must be assigned at compile time.

    const Pi = 3.14 const Greeting = "Hello, Go!"


5. Data Types

  • Primitive Types: int, float64, bool, string
  • Composite Types: array, slice, map, struct

6. Functions

  • Defining a Function:

    func add(a int, b int) int {return a + b }

  • Calling a Function:

    result := add(5, 3) fmt.Println(result) // Output: 8


7. Conditional Statements

  • If-Else:

    if x > 10 { fmt.Println("x is greater than 10") } else if x == 10 { fmt.Println("x is 10") } else { fmt.Println("x is less than 10") }

  • Switch:

    switch day { case 1: fmt.Println("Monday") case 2: fmt.Println("Tuesday") default: fmt.Println("Another day") }


8. Loops

  • For Loop:

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

  • For-Range (used with collections):

    arr := []int{10, 20, 30} for index, value := range arr { fmt.Printf("Index: %d, Value: %d\n", index, value) }


9. Arrays and Slices

  • Array:

    var arr [3]int = [3]int{1, 2, 3} fmt.Println(arr) // Output: [1 2 3]

  • Slice:

    slice := []int{1, 2, 3} slice = append(slice, 4) fmt.Println(slice) // Output: [1 2 3 4]


10. Maps

m := make(map[string]int) m["age"] = 25 fmt.Println(m["age"]) // Output: 25


11. Pointers

var x = 10 var ptr = &x // 'ptr' stores the memory address of 'x' fmt.Println(*ptr) // Output: 10


12. Concurrency

  • Goroutines:

    go func() {fmt.Println("This runs in a goroutine") }()

  • Channels:

    ch := make(chan int) go func() { ch <- 5 }() fmt.Println(<-ch) // Output: 5


13. Error Handling

  • Using error Interface:

    import "errors" func divide(a, b int) (int, error) { if b == 0 {return 0, errors.New("division by zero") }return a / b, nil }


This covers the basic syntax of GoLang. Let me know if you'd like more detailed explanations or examples on any topic!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql