GoLand 2020.3 Help

How to use type parameters for generic programming

Type parameters were introduced by the following draft to support the 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(type T)(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.

Enable support for type parameters

  • Open settings by pressing Ctrl+Alt+S and navigate to Go and select the Enable generics (experimental support for type parameters) checkbox.

  • 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

Type parameters work only in GO2 files. Also, ensure that the package must not contain regular GO files. Otherwise, the go2go tool reports an error.

You can rename the file in the Project tool window by using the Rename refactoring (Shift+F6) or by using the intention action.

Convert the GO file to GO2

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

  • Click the type parameter in the editor, press Alt+Enter, and select Rename file to '<filename>.go2'.

    Enable the experimental support of type parameters

After these preparations, you can run your code. The easiest way is to use the go2go playground. If you want to run the code from GoLand, you need to compile the developer's version of Go SDK and prepare the go2go tool.

Run 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

As type parameters are in the state of a draft, their functionality is not included in the main SDK. To work with type parameters, you need to check out and compile the dev.go2go 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.go2go
  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.

    ./all.bash
    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/goroot).

    Point GOROOT to the compiled developer's version

To work with generics, GO SDK has the go2go tool. The go2go tool reads syntax in GO2 files and generates usual GO files with translated code from GO2 files. For more information about go2go, run go doc cmd/go2go in the Terminal tool window or the command prompt.

Step 3. Create the go2go external tool

  1. Open settings by pressing Ctrl+Alt+S and navigate to Tools | External Tools.

  2. Click the Add button (the Add button).

  3. In the Name field, type the name of the tool (for example, go2go).

  4. In the Program field, type the path to the Go executable (for example, /Users/jetbrains/goroot/bin/go). Also, you can click the Browse icon (the Browse button) and navigate to the directory with the Go executable.

  5. In the Arguments field, type the following arguments: tool go2go build.

  6. In the Working directory field, click the Add icon, and select the FileDir macro.

    Create the go2go external tool

Step 4. Run the go2go tool on GO2 files

  1. In the Project tool window (View | Tool Windows | Project), click a GO2 file.

  2. Press Ctrl+Shift+A to search for actions, and type the name of the external tool created at Step 3 (for example, go2go).

  3. Press Enter.

    The go2go tool generates a GO file and an executable. The GO file has mangled names with Odia (Oriya) digits. Do not edit the GO file directly. For more information about go2go, run go doc cmd/go2go in the Terminal tool window or in the command prompt.

    Run the go2go tool on GO2 files

Troubleshooting

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>.
Set GOROOT_BOOTSTRAP
Last modified: 19 August 2020