Going from Swift to Go: Method Signatures

Going from Swift to Go: Method Signatures

Part 2 of the Swift to Go series. There are a few syntax differences between Swift and Go with regards to method names/signatures, but beyond this they are very similar.

Function without parameters or return values

In both swift and Go, the basic function is the same; first declare ‘func’, then choose a function name followed by (), and finish with the content in brackets. Example:

func functionName() {
//content here
}

In Go, it is also possible to set the function like you would set a variable. We specify the name, use ‘:=’, then declare func followed by (). Example:

functionName := func() { //content here }

Function with 1 parameter

In Swift, to add a parameter, in () we add a type declaration followed by the variable name; in Go, we add the same things, but in opposite order. Example in Swift:

func functionName(Int varName) {
//content here
}

Example in Go:

func functionName(varName int) {
//content here
}

Function with multiple parameters

In both languages, we can add parameters by adding a comma after our first parameter, then using the same syntax for a second parameter.

Example in Swift:

func functionName(Int varName, bool secondVarName) {
//content here
}

Example in Go:

func functionName(varName int, secondVarName bool) {
//content here
}

In Go, if all the parameters are the same type, we only need to specify the type on the last parameter–the other parameters will be inferred to have the same type! Example:

func functionName(firstInt, secondInt int) {
//content here
}

Function with 1 return value

In both languages, any returned parameters/variables are declared after (). In Swift, we use -> to point to the return type; in Go, we can simply declare the return type. Example in Swift:

func functionName() -> Int {
return 3
}

Example in Go:

func functionName() int {
return 3
}

In Go, you can also set the return type to have a variable name. What use is this? We can set and alter this variable, and when the function returns, we don’t have to say ‘return this value’, the function will automatically return the variable. Example:

func functionName() (x int) {
x = 3
return
}

Here, the function will return 3, because we created x and told the function that this particular variable will be returned when the function is returned.

Function with multiple return values

To return multiple values, make sure your first return value is surrounded by (). After your first return value, declare an additional value the same way you declared the first. Example in Swift:

func functionName() -> (Int, Int) {
//content here
}

Example in Go:

func functionName() (int, int) {
//content here
}

Alternate example in Go:

func functionName() (x int, y int) {
//content here
}

Similar to parameters, if all your return types in go are the same and you name your variables, you only need to declare the type for the final value; all others will be inferred. Example:

func functionName() (x, y int) {
//content here
}

Functions with multiple parameters and return values

Here are a couple examples to showcase multiple parameters and return values in a method signature:

Example in Swift:

func functionName(Int x, Int y) -> (Int, Int) {
return (x + y, x * y)
}

Example in Go:

func functionName(x, y int) (a, b int) {
a = x + y
b = x * y
return
}