GoLand 2018.1 Help

Extract Constant

The Extract Constant refactoring makes your source code easier to read and maintain. It also helps you avoid using hardcoded constants without any explanations about their values or purpose.

  1. In the editor, select an expression or declaration of a variable you want to replace with a constant .
  2. Press Ctrl+Alt+C to introduce a constant or select Refactor | Extract | Constant.

    Select an expression you want to extract as constant and press Enter. Select the number of occurrences you want to replace and a name you want to use.

    go extract constant


Use caseBeforeAfter
Let's introduce a constant for the expression "1+2+3 =" that occurs twice throughout code.
func main() { i := 1 res := plus(i,2) fmt.Println("1+2+3 =", res) res =plusPlus(1,2,3) fmt.Println("1+2+3 =", res) }
func main() { i := 1 res := plus(i, 2) const s = "1+2+3 =" fmt.Println(s, res) res = plusPlus(1, 2, 3) fmt.Println(s, res) }
Last modified: 16 April 2018