Go Function Parameter Type

Introduction

A function can take zero or more typed arguments.

The type comes after the variable name.

Functions can be de?ned to return any number of values that are always typed.

package main //from  w  ww .  ja  v a  2 s . c om

import "fmt" 

func add(x int , y int) int { 
     return x + y 
} 

func main() { 
     fmt.Println(add(42, 13)) 
} 

In the following example, instead of declaring the type of each parameter, we only declare one type that applies to both.

package main // w w w  .  ja  v a  2 s.  c  o m

import "fmt" 

func add(x,  y int) int  { 
     return x  + y 
} 

func main() { 
     fmt.Println(add(42,   13)) 
} 



PreviousNext

Related