reCAPTCHA WAF Session Token
Go

Go Programming Language: Hands-on Examples for Developers

Go, also known as Golang, is a statically typed, compiled programming language developed by Google. It is designed to be efficient, reliable, and easy to use. With its simple syntax and powerful features, Go has gained popularity among developers for developing web applications, cloud services, and system tools.

In this article, we will explore some hands-on examples of Go programming language for developers to get started with.

1. Hello World Program:

Let’s start with the classic “Hello, World!” program in Go. Create a new file named hello.go and add the following code:

“`

package main

import “fmt”

func main() {

fmt.Println(“Hello, World!”)

}

“`

To run the program, open a terminal window, navigate to the directory where the hello.go file is located, and type the following command:

“`

go run hello.go

“`

You should see the output “Hello, World!” printed to the terminal.

2. Variables and Data Types:

In Go, variables are declared using the var keyword followed by the variable name and type. Here’s an example of declaring and using variables in Go:

“`

package main

import “fmt”

func main() {

var name string = “John”

age := 30

fmt.Printf(“Name: %s, Age: %d\n”, name, age)

}

“`

In this example, we declare a string variable called name and an integer variable called age.

3. Functions:

Functions in Go are defined using the func keyword followed by the function name, parameters, and return type. Here’s an example of defining and calling a function in Go:

“`

package main

import “fmt”

func add(x int, y int) int {

return x + y

}

func main() {

result := add(5, 3)

fmt.Println(“Result:”, result)

}

“`

In this example, we define a function called add that takes two integer parameters and returns their sum.

4. Loops and Control Structures:

Go supports traditional for loops, if-else statements, and switch statements. Here’s an example of using loops and control structures in Go:

“`

package main

import “fmt”

func main() {

for i := 1; i

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock