in tools/go-agent/tools/dst.go [377:413]
func findFirstNoImportLocation(fset *token.FileSet, file *ast.File, fileContent *bytes.Buffer) (int, error) {
var pos token.Pos
for _, decl := range file.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if genDecl.Tok == token.IMPORT {
pos = genDecl.End()
continue
}
}
break
}
importEndLine := fset.Position(pos).Line
if pos == 0 {
if len(file.Decls) == 0 {
return 1, nil
}
importEndLine = fset.Position(file.Decls[0].Pos()).Line
}
lineNumber := 0
for {
line, err := fileContent.ReadBytes('\n')
if err != nil {
return 0, err
}
lineNumber++
if lineNumber < importEndLine {
continue
}
trimed := strings.TrimSpace(string(line))
if trimed == "" || trimed == ")" ||
(strings.HasPrefix(trimed, "import ")) ||
(packageImportExp.MatchString(trimed)) {
continue
}
return lineNumber, nil
}
}