Skip to content

evanj/loopcheck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loop Check: Check go loops for escaping variables

Analyzes Go source code to find places where a pointer to a loop variable is created. This is a common bug. See my blog post for details.

Usage

  1. go install github.com/evanj/loopcheck
  2. $GOPATH/bin/loopcheck (source code or package)

Example

Code (go playground):

package main

import "fmt"

func main() {
  values := []MyStruct{MyStruct{1}, MyStruct{2}, MyStruct{3}}
  output := []*MyStruct{}
  for _, v := range values {
    output = append(output, &v)
  }
  fmt.Println("output:", output)
}

type MyStruct struct {
  number int
}

func (m *MyStruct) String() string {
  return fmt.Sprintf("MyStruct{%d}", m.number)
}

Output:

example1.go:9: takes address of loop variable: &v
  range at line 8: for _, v := range values

See Also

Inspired by errcheck: https://github.com/kisielk/errcheck

Also inspired by the range check code in go vet: https://github.com/golang/tools/blob/master/cmd/vet/rangeloop.go

About

Checks Go source code for a common bug: accidentally taking the address of a loop variable

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages