create a function in go

In Go programming language, you can create a function using the func keyword followed by the function name, parameters inside the parentheses (if any), and the function body. Here's an example of a simple function that takes two string parameters and returns their concatenation:

main.go
func concatenateStrings(str1 string, str2 string) string {
    return str1 + str2
}
84 chars
4 lines

In the above code, func is the keyword to declare a function, concatenateStrings is the function name, str1 and str2 are the parameter names with their respective data types, and string is the return type of the function.

To call this function, you simply need to pass two string values to it, like this:

main.go
result := concatenateStrings("Hello ", "World!")
fmt.Println(result) // Output: Hello World!
93 chars
3 lines

In this case, the result will be the concatenated string "Hello World!" displayed on the console using the Println() function.

gistlibby LogSnag