Set function parameter types in Go

I start with the Go programming language, and I try to determine the parameter types of the Go function called addStuff , which simply adds two integers and returns their sum, but I see the following error when I try to compile the function:

 prog.go:6: undefined: a prog.go:6: undefined: b prog.go:7: undefined: a prog.go:7: undefined: b prog.go:7: too many arguments to return prog.go:11: addStuff(4, 5) used as value 

Here is the code that caused this compiler error:

 package main import "fmt" import "strconv" func addStuff(a, b){ return a+b } func main() { fmt.Println("Hello," + strconv.Itoa(addStuff(4,5))) } 

What am I doing wrong here, and what is the correct way to set parameter types in Go?

+7
source share
1 answer
 func addStuff(a int, b int) int { return a+b } 

This will result in parameters a and b type int and will return an int function. An alternative is func addStuff(a, b int) int , which will also execute parameters a and b type int .

I highly recommend the Tour of Go , which teaches the basics of Go.

+26
source

All Articles