×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Miscellaneous Operators

By IncludeHelp Last updated : October 05, 2024

Golang 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.