A simple shell script to generate cscope index file for go

1,063 views
Skip to first unread message

Neo Liu

unread,
Apr 9, 2014, 11:32:00 AM4/9/14
to golan...@googlegroups.com
Cscope also works for go language. So I just write a simple shell script to generate cscope index file for go.

The script can be downloaded from my github


Use this script is simple, too. Just run it under your project's root directory.


~/go/src/github.com/diabloneo/hello$ ll
total 4
-rw-r--r-- 1 diabloneo diabloneo 474 Apr 9 00:07 hello.go
~/go/src/github.com/diabloneo/hello$ echo $GOROOT
/opt/go
~/go/src/github.com/diabloneo/hello$ echo $GOPATH
/home/diabloneo/go
~/go/src/github.com/diabloneo/hello$ cscope-go.sh
Done
~/go/src/github.com/diabloneo/hello$ ll
total 7512
-rw-r--r-- 1 diabloneo diabloneo 49099 Apr 9 23:19 cscope.files
-rw-r--r-- 1 diabloneo diabloneo 7638765 Apr 9 23:19 cscope.out
-rw-r--r-- 1 diabloneo diabloneo 474 Apr 9 00:07 hello.go
~/go/src/github.com/diabloneo/hello$ cscope -d -l -0 LookupHost
/opt/go/src/pkg/net/hosts_test.go TestLookupHost 74 addrs, _ := LookupHost("localhost")
/opt/go/src/pkg/net/lookup.go LookupHost 24 func LookupHost(host string) (addrs []string, err error) {
/opt/go/src/pkg/net/lookup_plan9.go lookupIP 119 addrs, err := LookupHost(host)


And the content of the script:

#!/bin/bash
# generate cscope index files in current directory
# the generated cscope index files also include go standard packages
if [ $GOROOT = "" ]
then
    echo "GOROOT is not set"
    exit 1
fi
go_pkg_src=$GOROOT/src/pkg
find $go_pkg_src -name "*.go" -print > cscope.files
if cscope -b -k; then
    echo "Done"
else
    echo "Failed"
    exit 1
fi

Hope you like it.

Aram Hăvărneanu

unread,
Apr 9, 2014, 11:56:57 AM4/9/14
to Neo Liu, golang-nuts
Thanks, but Roger Peppe's godef is better because it's syntactic, not lexical:

http://godoc.org/code.google.com/p/rog-go/exp/cmd/godef

It works great in acme and if you don't use acme, there are vim/emacs
plugins distributed with the standard distribution.

--
Aram Hăvărneanu
Message has been deleted

Neo Liu

unread,
Apr 9, 2014, 11:25:28 PM4/9/14
to golan...@googlegroups.com
The original script missed one 'find' line.
 
#!/bin/bash

# generate cscope index files in current directory
# the generated cscope index files also include go standard packages

if [ $GOROOT = "" ]
then
echo "GOROOT is not set"
    exit 1
fi

go_pkg_src=$GOROOT/src/pkg

find $go_pkg_src -name "*.go" -print > cscope.files
find . -name "*.go" -print >> cscope.files

Neo Liu

unread,
Apr 9, 2014, 11:31:23 PM4/9/14
to golan...@googlegroups.com

Yes, godef is better on find definition location, and works fine with https://github.com/dgryski/vim-godef plugin

But cscope can find strings with regexp. I think cscope and godef can work together.

Tamás Gulácsi

unread,
Apr 10, 2014, 12:32:50 AM4/10/14
to golan...@googlegroups.com
Don't require GOROOT to be set, instead use the output of "go env".
Reply all
Reply to author
Forward
0 new messages