OneBite.Dev - Coding blog in a bite size

declare a simple function in Go

Code snippet on how to declare a simple function in Go

  package main
 
  func greet() string {
	  return "Hello World!"
  }

This code is an example of declaring a simple function in Go. First, it includes the package main, which is the top-level package for all Go programs. Next, the function greet is declared. This is done by putting func in front of the function name, followed by a pair of parentheses. Inside the function, a string is returned with the value “Hello World!”. Finally, the end of the function has the key word return, which tells the program to stop running the function and instead return the value of the string.

go