chan1.go is a Go program that demonstrates the use of goroutines and channels for concurrent programming. The program consists of three main functions: main, runchannels, and getNumber.
The main function is the entry point of the program. It creates a channel chan1
with a buffer size of 1 and starts two goroutines: runchannels and getNumber. The runchannels goroutine calculates the hash of numbers from 0 to 99 and writes the results to the channel, while the getNumber goroutine reads from the channel and prints the results. The main function then sleeps for 2 seconds to allow the goroutines to complete.
The runchannels function calculates the hash of numbers from 0 to 99 using the hash function and writes the results to the channel. It uses a for loop to iterate over the numbers and writes each result to the channel using the <-
operator.
The getNumber function reads from the channel and prints the results. It uses a for loop with the range
keyword to iterate over the values received from the channel and prints each value using fmt.Println
.
The hash function calculates the hash of a given string using the FNV-1a hash algorithm. It creates a new hash object using fnv.New32a()
and writes the string to the hash object using h.Write([]byte(s))
. The hash value is then returned using h.Sum32()
.
Overall, chan1.go demonstrates the use of goroutines and channels for concurrent programming in Go, as well as the use of the FNV-1a hash algorithm for hashing strings.