Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang function parameter without type?

Tags:

syntax

go

func Match(pattern, name string) (matched bool, err error)
  • https://golang.org/pkg/path/filepath/#Match

Why does pattern not have to have a type (like pattern string)?

like image 882
nodakai Avatar asked Jun 13 '16 02:06

nodakai


People also ask

How do you pass a function as a parameter in Go language?

Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference. By default, Golang uses the call by value way to pass the arguments to the function.

Can you pass a type Golang?

You can't. You can only pass a value, and CustomStruct is not a value but a type. Using a type identifier is a compile-time error. (Also don't forget that this returns an empty string for anonymous types such as []int .)

What is type T in Golang?

T is a new kind of parameter in Go: a type parameter. We say that PrintAnything[T] is a parameterized function, that is, a generic function on some type T.

How do you write functions in Golang?

In Golang, we declare a function using the func keyword. A function has a name, a list of comma-separated input parameters along with their types, the result type(s), and a body. The input parameters and return type(s) are optional for a function. A function can be declared without any input and output.


1 Answers

Per https://tour.golang.org/basics/5:

When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

In this example, we shortened

x int, y int

to

x, y int

like image 119
user94559 Avatar answered Oct 19 '22 15:10

user94559