Golang Miscellaneous Operators

Here, we are going to learn about the Miscellaneous Operators in the Go programming language with examples.
Submitted by IncludeHelp, on December 08, 2021

Miscellaneous Operators

There are a few other special operators which are used in the Golang. Here is the list of some other/miscellaneous operators which are used in the Golang.

List of Golang Miscellaneous Operators

Operator Description Example
x:=5
y:=3
Address Of (&) Returns the address of a variable. &x
Pointer to a variable (*) Returns the value of a pointer variable. *x
Receive (<-) This operator is used to receive a value from the channel. y := <-ch

Example of Golang Miscellaneous Operators

The below Golang program is demonstrating the example of miscellaneous operators.

// Golang program demonstrate the
// example of assignment operators

package main

import "fmt"

func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	c <- sum // send sum to c
}

func main() {
	x := 5

	// Address Of operator
	ptr := &x
	fmt.Println(*ptr)

	// Pointer to variable
	*ptr = 10
	fmt.Println(x)

	// Channel Example
	intslice := []int{7, 2, 8, -9, 4, 0}

	chnl := make(chan int)
	go sum(intslice[:len(intslice)/2], chnl)
	go sum(intslice[len(intslice)/2:], chnl)
	a, b := <-chnl, <-chnl // receive from chnl

	fmt.Println(a, b, a+b)
}

Output:

5
10
-5 17 12



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.