GoLand 2020.3 Help

Using the Generate action

Generate a constructor

The Generate constructor action generates a function that creates values of a struct type. For more information about constructors, see Constructors and composite literals from the Effective Go manual.

  1. Place the caret at your code, and press Alt+Insert. Alternatively, you can use intention actions Alt+Enter.

  2. In the Generate window, select Generate constructor.

  3. In the Select Fields dialog, select fields that you want to use in the constructor.

  4. Click OK.

    Generate a constructor

Generate getter and setter

You can generate getters and setters separately or use one action to generate both methods. For more information about constructors, see Getters from the Effective Go manual.

  1. Place the caret at your code, and press Alt+Insert. Alternatively, you can use intention actions Alt+Enter.

  2. In the Generate window, select Getter and setter.

    Generate getter and setter

Implement missing methods

To implement an interface in Go, you need to implement all the methods that are listed in the interface. In GoLand, you can automatically generate templates for those methods.

With the Implement Methods action, you can specify parameter names even if they are omitted in the interface definition.

  1. Place the caret at your code, and press Alt+Insert.

  2. In the Generate window, select Implement Methods ( or press Ctrl+I ).

  3. In the Choose Type window, select the type that you want to use. Alternatively, click Create Type to create a new type.

  4. In the Choose interface to implement field, type a name of the interface that you want to implement.

  5. Replace the panic("implement me") line with a code for the method.

    Example of go generate

Generate an empty test file

  1. Place the caret at your code, and press Alt+Insert.

  2. In the Generate window, select Empty test file.

Generate an empty test file

Generate tests for files, packages, and functions

  1. Place the caret at your code, and press Alt+Insert.

  2. In the Generate window, select Tests for file, Tests for package, or Test for function.

    Generate tests for files and packages
  1. Open Goland settings Ctrl+Alt+S and navigate to Editor | Copyright | Copyright Profiles.

  2. Click the Add icon Add icon.

  3. In the Create Copyright Profile dialog, type a name for a copyright profile.

  4. Click OK.

  5. Place the caret at your code, and press Alt+Insert.

  6. In the Generate window, select Copyright.

Last modified: 08 March 2021