Home > Article > Backend Development > Golang function return value type derivation mechanism
In the Go language, a function return value type derivation mechanism exists. The compiler deduce the return value type according to rules: Multiple return values: Each return value deduce the actual type. The single return value is untyped: the same type as the actual value is deduced. No return value: void type is deduced.
In the Go language, functions can declare multiple return value types, including basic types, structural types, Error types and so on. However, sometimes we do not need to explicitly specify the return value type, but can let the compiler infer the return value type. This process is called type inference.
The compiler deduce the return value type according to the following rules:
void
type. Example 1: Multiple value return
func getPersonDetails() (string, int, bool) { return "John Doe", 30, true }
In this example, the compiler will deduce the following return value type :
string
int
Example 2: Single value return
func getFirstName() string { return "John" }The compiler will deduce the
string type.
Example 3: No return value
func printMessage() { fmt.Println("Hello") }The compiler will infer the
void type.
The above is the detailed content of Golang function return value type derivation mechanism. For more information, please follow other related articles on the PHP Chinese website!