Syntax

Go Method on a Struct Syntax

In Go, method on a struct: attaches a method to a type — func (u *User) Greet() string { return "Hi " + u.Name }

Quick Answer

Syntax
func (u *User) Greet() string { return "Hi " + u.Name }
GoSyntax

Method on a Struct

Code example
func (u *User) Greet() string { return "Hi " + u.Name }

Attaches a method to a type.

Learn More

How to method on a struct in Go

  1. Open your Go file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Go Method on a Struct Syntax

Use func (u *User) Greet() string { return "Hi " + u.Name }.

Related Syntax References

Frequently asked questions

What is the Go syntax for method on a struct?

Use this pattern: func (u *User) Greet() string { return "Hi " + u.Name }

Can I copy this Go code example?

Yes. Use Copy code and adapt the example to your program.

Is this Go syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify