GoLand 2021.1 Help

Introduce type

The Introduce Type refactoring extracts a type that is inside of another type. Also, you can use this refactroing for converting the anonymous structure type to a named type.

For example, the extract operation might look like this. Consider the following JSON.

{ "email": "noah.poulsen@example.com", "gender": "male", "phone_number": "53705244", "birthdate": 1314124015, "location": { "street": "7998 oddenvej", "city": "nr åby", "state": "syddanmark", "postcode": 73617 }, "username": "purplesnake503", "password": "zelda", "first_name": "noah", "last_name": "poulsen", "title": "mr", "picture": "men/41.jpg" }

If you paste it to GoLand's editor and convert it to a struct type, you will get the T struct type with another Location struct type inside. The T struct is valid and you can continue working with it. But you might want to embed the Location type differently. See the following animation.

extract type refactoring
  1. In the editor, click a type that you want to extract.

  2. Click Refactor | Extract/Introduce and select Introduce Type.

    The animation shows how GoLand converts the anonymous structure type to a named type. You can try the Introduce Type refactoring with the following code snippet.

    package main func main() { type DemoType struct { Field1 string Field2 int } _ = DemoType{ Field1: "Hello", Field2: 42, } }
    anonymous structure type to named type
Last modified: 08 March 2021