Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use the `go` keyword when calling a function?

Tags:

go

I was going through an example of a TCP server. They defined a function and called it with:

go handleRequest(conn) 

I thought it was weird seeing the go keyword, so I tried it without:

handleRequest(conn) 

To my surprise, this worked!

  • If both work the same way, why use the go keyword at all?
  • If they work differently, what is the difference?
  • Is there a certain style guideline to use, or should you just use personal preference?
like image 213
hmatt1 Avatar asked Sep 24 '14 00:09

hmatt1


People also ask

What does the Go keyword do?

Concurrency is not well documented in the go spec and it is one of the most powerful features of the language, the go keyword is the starting point when you are building concurrent software and not procedural software in go.

Why make function is used in Go?

Golang make() is a built-in slice function used to create a slice. The make() function takes three arguments: type, length, and capacity, and returns the slice. To create dynamically sized arrays, use the make() function. The make() function allocates a zeroed array and returns a slice that refers to that array.

How do you use functions in Go?

Working with Parameters In Go, you must specify the data type for each parameter. Let's create a program that repeats a word a specified number of times. It will take a string parameter called word and an int parameter called reps for the number of times to repeat the word.

What is Go keyword in Java?

The Go goto statement is a jump statement which is used to transfer the control to other parts of the program. In goto statement, there must be a label. We use label to transfer the control of the program. Go Goto Statement Example: package main.


1 Answers

go starts a goroutine, which is managed by golang run-time.

It can either run on the current OS thread, or it can run on a different OS thread automatically.

You can refer to basic golang documents for this, for example, one item in Google search keyword goroutine is golang concurrency.

like image 67
WiSaGaN Avatar answered Oct 20 '22 11:10

WiSaGaN