Go · Stage 9 – Organizing Bigger Programs · Lesson 51 of 64

Creating Your Own Package

Move related reusable code into its own package.

Go code

package greeting

func Hello() string {
    return "Hello"
}
Line-by-line explanation
  1. The file belongs to package greeting.
  2. Hello begins uppercase, so it is exported.
  3. The function returns text.
Expected output
(no output)

A package definition does not print by itself.

Place this file in a greeting directory inside the module. All ordinary Go files in that directory use the same package name.