FAQ
How do you find the definition of a function or a type without having to go
through all the package files? This appears a problem quite often esp when
browsing code on the web. You know which package something is defined in,
but not which file if the package has more than one files.

I use Emacs. For my own code I plan to keep most definitions for a package
in a single file except for maybe doc.go and test_xxx.go.

--

Search Discussions

  • Andrew Gerrand at Jan 10, 2013 at 2:21 am

    On 10 January 2013 12:55, wrote:

    How do you find the definition of a function or a type without having to
    go through all the package files?

    This is how I do it:
    grep '^func Foo' *.go

    --
  • Rob Pike at Jan 10, 2013 at 2:24 am
    Let me spruik a few shell scripts I have.

    t finds a type.

    % cat t
    #!/bin/sh

    9 grep -n '^type '$1' ' *.go
    %

    f finds a function or method.


    % cat f
    #!/bin/sh

    9 grep -n '^func (\([^)]+\) )?'$1'\(' *.go
    %

    These use the Plan 9 (port) grep but you may adjust as needed to use
    greps of impure design.

    The big step is to install and set up Russ Cox's codesearch stuff
    (http://code.google.com/p/codesearch/). Then come the fun ones:

    ct finds a type in any installed (and indexed) go file.

    % cat ct
    #!/bin/sh

    csearch -n -f '\.go$' '^type '$1
    %

    cf does the same for functions and methods.

    % cat cf
    #!/bin/sh

    csearch -n -f '\.go$' '^func (\([^)]+\) )?'$1'\('
    %

    For instance:

    % cf Println
    /Users/r/go/src/cmd/vet/main.go:142:func Println(args ...interface{}) {
    /Users/r/go/src/pkg/fmt/print.go:287:func Println(a ...interface{}) (n
    int, err error) {
    /Users/r/go/src/pkg/log/log.go:168:func (l *Logger) Println(v
    ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
    /Users/r/go/src/pkg/log/log.go:280:func Println(v ...interface{}) {
    % cd fmt
    /Users/r/go/src/pkg/fmt
    % t pp
    print.go:106: type pp struct {
    %

    By the way, it's worth setting up CDPATH as well - helps a lot.

    % echo $CDPATH
    .:$HOME:$HOME/go/src/pkg/:$HOME/src/code.google.com/p
    bismarck=%

    --
  • Speech Free at Jan 10, 2013 at 6:35 am
    Thanks for the nice tutorial!
    On Wednesday, January 9, 2013 6:24:24 PM UTC-8, Rob Pike wrote:

    Let me spruik a few shell scripts I have.
    --
  • Antonio Dolcetta at Jan 10, 2013 at 2:25 am

    On 01/10/2013 09:55 AM, speech.free@gmail.com wrote:
    How do you find the definition of a function or a type without having
    to go through all the package files? This appears a problem quite
    often esp when browsing code on the web. You know which package
    something is defined in, but not which file if the package has more
    than one files.

    I use Emacs. For my own code I plan to keep most definitions for a
    package in a single file except for maybe doc.go and test_xxx.go.
    --
    I use grep, or ack.
    Admittedly it does not help much when browsing on the web, but if you
    look at the godoc generated documentation clicking through on the
    type/function brings you to the code itself, so I do that instead of
    looking at it in github.

    Antonio

    --
  • Speech Free at Jan 10, 2013 at 6:16 am
    Thanks for the tip. Due to the near uniform font colors I didn't realize
    that one can click through function/type name parts of the standard
    packages. That certainly helps with the standard packages. To make this
    more obvious for others it would be nice if the hyperlinks extend through
    the keywords (func/type) or use a distinct style to show where the links
    are.
    On Wednesday, January 9, 2013 6:25:04 PM UTC-8, Antonio Dolcetta wrote:
    On 01/10/2013 09:55 AM, speec...@gmail.com <javascript:> wrote:
    How do you find the definition of a function or a type without having
    to go through all the package files? This appears a problem quite
    often esp when browsing code on the web. You know which package
    something is defined in, but not which file if the package has more
    than one files.

    I use Emacs. For my own code I plan to keep most definitions for a
    package in a single file except for maybe doc.go and test_xxx.go.
    --
    I use grep, or ack.
    Admittedly it does not help much when browsing on the web, but if you
    look at the godoc generated documentation clicking through on the
    type/function brings you to the code itself, so I do that instead of
    looking at it in github.

    Antonio
    --
  • David DENG at Jan 10, 2013 at 8:04 am
    For open source project, I ctrl+f in godoc.

    David
    On Thursday, January 10, 2013 9:55:49 AM UTC+8, speec...@gmail.com wrote:

    How do you find the definition of a function or a type without having to
    go through all the package files? This appears a problem quite often esp
    when browsing code on the web. You know which package something is defined
    in, but not which file if the package has more than one files.

    I use Emacs. For my own code I plan to keep most definitions for a package
    in a single file except for maybe doc.go and test_xxx.go.
    --
  • Guelfey at Jan 10, 2013 at 8:16 am
    If you run a local godoc server (or use godoc.org), clicking the function or type definition shows you the definition in the code.

    --
  • DisposaBoy at Jan 10, 2013 at 6:06 pm
    ctrl+dot, ctrl+l then start typing part of its name .. on Sublime Text with GoSublime

    --
  • Nick Craig-Wood at Jan 10, 2013 at 8:42 pm

    On 10/01/13 01:55, speech.free@gmail.com wrote:
    How do you find the definition of a function or a type without having to
    go through all the package files? This appears a problem quite often esp
    when browsing code on the web. You know which package something is
    defined in, but not which file if the package has more than one files.

    I use Emacs. For my own code I plan to keep most definitions for a
    package in a single file except for maybe doc.go and test_xxx.go.
    Use `M-x grep` is what I do in emacs. This then opens a buffer you can
    use to jump to the found lines with Enter, or you can use `M-g n` and
    `M-g p` from a different buffer

    I use `M-x grep` so much that I keep it bound to F4 ;-)

    http://emacswiki.org/emacs/GrepMode


    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 10, '13 at 2:14a
activeJan 10, '13 at 8:42p
posts10
users8
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase