GoLand 2021.2 Help

How to use type parameters for generic programming

By using type parameters, you can write functions that handle incoming parameters without depending on the type specified in the function declaration.

For example, the PrintSlice function receives a slice of integers and prints it.

func PrintSliceInts(i []int) { for _, v := range i{ print(v) } } func main() { PrintSliceInts([]int {1,2,3,4,5,6,7,8,9}) }

To apply the same functionality to a slice of strings, you need to copy and paste the code of PrintSliceInts and make a new function called PrintSliceStrings, where everything is the same except for the signature.

You can rewrite the function and reuse your code. All you need is to introduce the type parameter and change the function parameter in the signature.

func PrintSlice[T any](s []T) { for _,v := range s{ print(v) } } func main() { PrintSlice([]int{1,2,3,4,5,6,7,8,9}) PrintSlice([]string {"a","b","c","d"}) }

Enable the experimental support of type parameters

By default, the support of type parameters is disabled. You can enable it in the settings or by using the intention action.

  1. Open settings by pressing Ctrl+Alt+S and navigate to Go.

  2. Select the Enable generics (experimental support for type parameters) checkbox.

  3. Click the type parameter in the editor, press Alt+Enter, and select Enable generics (experimental support for type parameters).

    Enable the experimental support of type parameters

GoLand 2021.2 and later versions support type parameters in GO files. It means that the IDE acknowledges that you use type parameters in your code and processes your code correspondingly. Though you still might see red code when passing values to a function with type parameters.

Run code in the go2go playground

To run your code, you need to convert a GO file to a GO2 file. You can rename the file in the Project tool window by using the Rename refactoring (Shift+F6).

Step 1. Convert the GO file to GO2

  • In the Project tool window (View | Tool Windows | Project), click the file and press Shift+F6.

    Enable the experimental support of type parameters

You can open go2goplay.golang.org and paste your code here. Or, use the Share in Playground action.

Step 2. Run your code in the go2go playground

  1. In the editor, press Ctrl+Shift+A to search for actions.

  2. Type Share in Playground and press Enter. Also, you can use the following shortcut in the editor: Ctrl+Alt+Shift+S.

  3. At go2goplay.golang.org, press the Run button.

    Run the code in go2go playground

Run code in GoLand

To work with type parameters, you need to check out and compile the dev.typeparams branch.

Step 1. Compile developer's version of the Go SDK

  1. Clone the Go repository to the goroot directory.

    git clone https://go.googlesource.com/go goroot
  2. Navigate to the goroot directory.

    cd goroot
  3. Check out the dev.go2go branch.

    git checkout dev.typeparams
  4. Navigate to the src directory.

    cd src
  5. Compile the Go SDK. In case of an error with GOROOT_BOOTSTRAP, see the Troubleshooting section or run the command with the GOROOT_BOOTSTRAP variable.

    Compile the Go SDK for generics

Step 2. Point GOROOT to the compiled developer's version

  1. Open settings by pressing Ctrl+Alt+S and navigate to Go | GOROOT.

  2. From the GOROOT list, select Go devel <commit_identifier>. If this version is not detected, click the Add button, select Local, and navigate to the directory with the compiled developer's version (for example, /Users/jetbrains/GolandProjects/goroot).

    Point GOROOT to the compiled developer's version

Step 3. Create the go.mod file

  • In the Terminal tool window (View | Tool Windows | Terminal), type the following command:

    go mod init main
    Create the go.mod file

Step 4. Create a run/debug configuration

  1. Navigate to Run | Edit Configurations.

  2. Click the Add button and select Go Build.

  3. In the Go tool arguments field, type the following arguments: -gcflags -G=3.


Step 5. Run your application

  1. Navigate to Run | Run. Alternatively, press Shift+F10.

  2. Select the run/debug configuration that you created on Step 4 and press Enter.



ErrorPossible solution
ERROR: Cannot find <path_to_go>. Set $GOROOT_BOOTSTRAP to a working Go tree >= Go <version>.

Open settings Ctrl+Alt+S and navigate to Tools | Terminal. In the Environment Variables field, click the Browse button the Browse button and add the following variable: GOROOT_BOOTSTRAP = <path_to_go_sdk>.

Last modified: 20 August 2021