Search for:

Select Statement in Go Language

The select statement is a powerful feature in Go that allows you to work with multiple channels concurrently. It enables you to wait for multiple channel operations and choose the first one that becomes ready. It’s often used in scenarios where you need to coordinate communication between different goroutines using channels.

Here’s the syntax of the select statement:

select {
case <-channel1:
    // Do something when channel1 is ready
case data := <-channel2:
    // Do something with data from channel2
case channel3 <- value:
    // Send value to channel3
    // Do something when no channel operation is ready

Key points to understand about the select statement:

  1. The select statement lets you wait for multiple channel operations simultaneously.
  2. If multiple channels are ready, one of them is chosen at random.
  3. The <- operator is used to receive data from a channel, and channel <- value is used to send data to a channel.
  4. The default case is executed when no other channel operation is ready.
  5. The select statement is non-blocking; it only blocks if all channels are unbuffered and none of them are ready.

Here’s a simple example of using the select statement:

package main

import (

func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)

	go func() {
		ch1 <- 42

	go func() {
		time.Sleep(2 * time.Second)
		ch2 <- 99

	select {
	case data := <-ch1:
		fmt.Println("Received from ch1:", data)
	case data := <-ch2:
		fmt.Println("Received from ch2:", data)
	case <-time.After(3 * time.Second):

Output (may vary due to random selection):

Received from ch1: 42

In this example, two goroutines send data to two different channels after different time intervals. The select statement waits for the first channel to become ready. Since the channel associated with the first goroutine is ready before the other, the <-ch1 case is executed.

The select statement is a fundamental tool for coordinating communication between goroutines efficiently, enabling you to create powerful and responsive concurrent programs.

Go Operators

In Go, operators are symbols that perform operations on variables and values. They allow you to manipulate data and perform various computations within your programs. Go supports a variety of operators, which can be classified into different categories based on their functionality. Here are the main categories of operators in Go:

Read More

Constants – Golang

In Go, constants are like variables, but their values cannot be changed or reassigned after they are declared. They are immutable, meaning their values remain constant throughout the program’s execution. Constants are useful when you have values that should not be modified to ensure consistency and avoid accidental changes.

In Go, you can declare constants using the const keyword followed by the constant’s name, its data type, and its value:

Read More

Identifiers in Go Language

In Go (Golang), identifiers are names used to identify various program entities, such as variables, constants, functions, types, packages, and labels. Identifiers are crucial for naming and referencing these entities within the program’s code. When naming identifiers, you need to follow certain rules and conventions:

Rules for Identifiers:

  1. Start with a Letter: An identifier must start with a letter (uppercase or lowercase). It cannot start with a digit or special characters.
  2. Contains Letters, Digits, and Underscores: After the first letter, an identifier can contain letters (both uppercase and lowercase), digits, and underscores (_). Special characters are not allowed.
  3. Case-Sensitive: Identifiers are case-sensitive, which means that myVar and myvar are considered different identifiers.
  4. Cannot be a Keyword: Identifiers cannot be the same as Go’s reserved keywords, which have specific meanings in the language.

Examples of Valid Identifiers:


Examples of Invalid Identifiers:

2ndNumber   // Identifier starts with a digit
@count      // Identifier contains a special character (@)
func        // Identifier is a reserved keyword

Go has a strong convention for naming identifiers to promote code readability and maintainability. Here are some common naming conventions:

  1. Use camelCase for variable and function names (e.g., userName, calculateSum).
  2. Use PascalCase for type names (e.g., Person, Product).
  3. Use ALL_CAPS for constants (e.g., PI, MAX_LENGTH).

It’s important to choose meaningful and descriptive names for identifiers to make the code more understandable for you and other developers who may work on the project in the future. By following the identifier naming conventions and rules, you can write clean, readable, and maintainable Go code.

Example identifiers in various contexts:

package main

import "fmt"

// Constants
const PI float64 = 3.14159
const maxIterations = 100

// Function to calculate the sum of two numbers
func add(a, b int) int {
    return a + b

// Custom type (struct)
type Person struct {
    Name string
    Age  int

func main() {
    // Variables
    var num1 int = 10
    var num2 int = 20

    // Using the add function and the constants
    result := add(num1, num2)
    fmt.Println("Result:", result)

    // Creating a Person struct and accessing its fields
    person := Person{Name: "John", Age: 30}
    fmt.Println("Name:", person.Name)
    fmt.Println("Age:", person.Age)

    // Printing the constant values
    fmt.Println("PI:", PI)
    fmt.Println("Max Iterations:", maxIterations)

In this complete Go program:

  1. We start with the package declaration package main, which indicates that this is the main package and serves as the entry point of the program.
  2. We import the required package (fmt) to use the Println function for printing output to the console.
  3. We define two constants PI and maxIterations, and we explicitly specify their data types.
  4. We define a function add that takes two integer parameters and returns their sum.
  5. We define a custom type Person using a struct, which has two fields (Name and Age).
  6. In the main function, we declare two variables num1 and num2, assign them values, and then use the add function to calculate their sum and print the result.
  7. We create a Person struct and initialize its fields (Name and Age). We then print the values of these fields.
  8. Finally, we print the values of the constants PI and maxIterations.

When you run this Go program, it will output:

Result: 30
Name: John
Age: 30
PI: 3.14159
Max Iterations: 100

This example demonstrates the usage of identifiers (PI, maxIterations, add, Person, num1, num2, result, person, etc.) in various contexts within a complete Go program.

pga77 pga77 slot suster123 kijangwin ladangtoto rajatogel slot-gopay kakek zeus scatter-hitam scatter-hitam kijangwin suster123 scatter-hitam slot taiwan slot-gopay hoki99 slot linkaja slot ovo nenektogel gasing777 tumi123 bento123 fendi188 gasing777 66kbet scatter-pink scatter-hijau ladangtoto ladangtoto gudangtoto rajatogel nusantara4d ladangtoto scatter-hitam slot-bsi scatter-hitam ladangtoto slot-bri pga77 slot-goceng slot-10k opaltogel scatter-hitam jp188 ladangtoto slot-linkaja slot-cimb slot-gopay slot77 sbankjago sbca slot-qris ladangtoto slot-dana idnslot slot-mandiri slot-maybank ladangtoto scatter-hitam ultra889 sumseltoto medantoto rajampo raja88jp indoslot88 asiatoto88 mpltoto wajik777 tumi123 kembangtoto alexistogel mawartoto rajatogel wajik777 madu303 mpo888 sikat88 gucitoto pga77 pga77 ladangtoto olx188 gacor88 ayah4d pga77 pga77 gemilang4d ladangtoto petir388 kembangtoto sikat88 ying77 slot212 texas88 slot138 rp888 rumah258 cuan88 slot-bonanza scatter-hitam slot-dana kakek-merah rajampo lampung4d alay4d jambitoto manadototo binjaitoto pajaktoto popotogel 888slot jacktoto kaskustoto keitogel laku77 wing4d bostoto laetoto afktoto rudaltoto ojktoto pga77 rajagacor olxslot rajatogel jacktoto bento4d kaskustoto pga77 preman69 mpo808 mpoxtra838 rajatogel rajagacor ladangtoto asiatoto tumi123 olx188 bro138 galaxy77 asiatogel88 gacor88 rajatogel slot77 padangtoto rasa4d sdtoto omtogel ltdtoto dentoto rupiahtoto gacor88 pgbet slot-thailand mawartoto rajatogel rajatogel rudaltoto mawartoto topjitu toto188 mom4d 888slot sdtoto angkasa168 hondatoto 888slot bro138 scatter-hitam slot-zeus kaskustoto rajatogel tumi123 mustang303 api5000 senja777 super33 dewanaga89 hokibet dewa234 sizi99 cuanwin77 liga2000 88mega popi99 bento123 macauslot 888slot hoki178 jackpot108 gadunslot gacor88 king999 rajampo big777 kedai69 rp888 rajapaito ladangtoto rajatogel gtr11 slot99bet alba88 stars77 mangga2bet klik88 rans288 fin4d ceriabet leoslot88 agen108 dewasurga88 dragon222 gacorbos88 medusa88 pga77 pga77 pga77 surgawin homebet88 bento4d pga77 pga77 jago777 sgpslot api88 m77casino m77slot rajatogel bento4d m77casino m77slot hoki178 rudaltoto