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.
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.
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
.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).
Type parameters work only in GO2 files. Also, ensure that the package does 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 ( ), click the file and press Shift+F6.
Click the type parameter in the editor, press Alt+Enter, and select Rename file to '<filename>.go2'.
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
In the editor, press Ctrl+Shift+A to search for actions.
Type
Share in Playground
and press Enter. Also, you can use the following shortcut in the editor: Ctrl+Alt+Shift+S.At go2goplay.golang.org, press the Run button.
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
Clone the Go repository to the goroot directory.
git clone https://go.googlesource.com/go gorootNavigate to the goroot directory.
cd gorootCheck out the
dev.go2go
branch.git checkout dev.go2goNavigate to the src directory.
cd srcCompile 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
Step 2. Point GOROOT to the compiled developer's version
Open settings by pressing Ctrl+Alt+S and navigate to
.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).
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
Open settings by pressing Ctrl+Alt+S and navigate to
.Click the Add button ().
In the Name field, type the name of the tool (for example,
go2go
).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 () and navigate to the directory with the Go executable.
In the Arguments field, type the following arguments:
tool go2go build
.In the Working directory field, click the Add icon, and select the FileDir macro.
Step 4. Run the go2go tool on GO2 files
In the Project tool window ( ), click a GO2 file.
Press Ctrl+Shift+A to search for actions, and type the name of the external tool created at Step 3 (for example, go2go).
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.
Troubleshooting
Error | Possible 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 Environment Variables field, click the Browse button and add the following variable: GOROOT_BOOTSTRAP = <path_to_go_sdk> . | . In the