Source code highlighting links on golang.org

336 views
Skip to first unread message

Joshua Liebow-Feeser

unread,
Aug 21, 2014, 8:14:43 AM8/21/14
to golan...@googlegroups.com
At present, when you click on a function or type in the golang.org/pkg documentation, it brings you to the source file with the relevant source code highlighted. However, these links are relative to the current published source, and when a source file changes, the links become invalid (often pointing to the wrong line or highlighting the wrong parts of lines). In light of the fact that references to source code are all over the internet (especially in programming forums like StackOverflow), might it be a good idea to have these links instead be permalinks to that version of the file, or at least have some way to get a permalink instead of a relative link for these sorts of purposes?

Cheers,
Josh

Jan Mercl

unread,
Aug 21, 2014, 8:32:23 AM8/21/14
to Joshua Liebow-Feeser, golang-nuts
Example of a permalink for a particular place of a particular revison:
https://code.google.com/p/go/source/browse/src/pkg/bytes/bytes.go?r=3c1174dcac91e408595a580b37fd2972d3b96972#50

-j

Joshua Liebow-Feeser

unread,
Aug 21, 2014, 8:39:07 AM8/21/14
to Jan Mercl, golang-nuts
Yeah, but what about for golang.org links? The current default is, for example:
http://golang.org/src/pkg/fmt/print.go?s=7558:7607#L253

It'd be nice to be able to stay within the same domain and have permalinks. I mean, if the code itself isn't available on golang.org, that's obviously asking a bit much, but if the links exist, it'd be nice to be able to access them programmatically in the way that the current links are generated.

Andrew Gerrand

unread,
Aug 21, 2014, 9:57:54 PM8/21/14
to Joshua Liebow-Feeser, golang-nuts

On 21 August 2014 22:14, Joshua Liebow-Feeser <josh...@gmail.com> wrote:
At present, when you click on a function or type in the golang.org/pkg documentation, it brings you to the source file with the relevant source code highlighted. However, these links are relative to the current published source, and when a source file changes, the links become invalid (often pointing to the wrong line or highlighting the wrong parts of lines). In light of the fact that references to source code are all over the internet (especially in programming forums like StackOverflow), might it be a good idea to have these links instead be permalinks to that version of the file, or at least have some way to get a permalink instead of a relative link for these sorts of purposes?

It's a nice idea. It would be non-trivial to make it happen, though.

golang.org is served by an instance of godoc that has only one copy of the source. We'd need to make both godoc and the URLs it uses more complex to achieve this.

Andrew

Joshua Liebow-Feeser

unread,
Aug 21, 2014, 10:05:07 PM8/21/14
to Andrew Gerrand, golang-nuts
Ah, I was afraid that might be the case. Oh well.

Willa Walthall

unread,
Aug 22, 2014, 12:27:50 AM8/22/14
to golang-nuts
Why are the links to source from godoc off by 10?

http://golang.org/src/pkg/bufio/bufio.go?s=830:971#L21

Visiting this link the first time you'll notice it points to line 31
which is what's selected by the ?s=830:971 parameter. In fact the
following link will also display at line 31 if opened as the initial
link in a browser window.

http://golang.org/src/pkg/bufio/bufio.go?s=830:971#L400
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Willa Walthall

unread,
Aug 22, 2014, 1:01:06 AM8/22/14
to golang-nuts
Transforming the URL is pretty simple actually, excluding the off by
10 error which I'm ignoring and assuming it is a bug. Here's an
example.

http://play.golang.org/p/Bkca50tRlB

I made it a simple command.
https://github.com/ghthor/godocpermalink

Would this really be so difficult for godoc to generate these links?
It should know which version of the source code it is displaying, it's
in the footer element on the page. It could even generate links for
remote packages because it could query the repository to discover the
ref number from the repository stored in the $GOPATH and then generate
a URL based on the web server used(github, bitbucket, etc).

Caleb Spare

unread,
Aug 22, 2014, 1:38:16 AM8/22/14
to Willa Walthall, golang-nuts
It's not an 'off by 10' error, it's that the javascript wants to
scroll the highlighted lines (s=830:971) to the top of the screen (the
first highlighted line is line 31). The #L21 links directly to line 21
within the document and the javascript scrollIntoView call happens
afterwards.

I'm not sure why the godoc links include the highlighting and the line
number. I think that's a bug.

On Thu, Aug 21, 2014 at 10:00 PM, Willa Walthall <ght...@gmail.com> wrote:
> Transforming the URL is pretty simple actually, excluding the off by
> 10 error which I'm ignoring and assuming it is a bug. Here's an
> example.
>
> http://play.golang.org/p/Bkca50tRlB
>
> I made it a simple command.
> https://github.com/ghthor/godocpermalink
>
> Would this really be so difficult for godoc to generate these links?

The source view is hosted at golang.org/src/pkg/, which does currently
not have old revisions of the code. You're proposing adding links to
code.google.com, but right now golang.org and godoc are nicely
self-contained and for the most part don't need to know about
code.google.com.

Andrew Gerrand

unread,
Aug 24, 2014, 10:45:38 PM8/24/14
to Willa Walthall, golang-nuts
That's true, but then we'd miss out on the various analysis results that are presented in source view. We haven't got this running on golang.org yet, but that's the plan.
Reply all
Reply to author
Forward
0 new messages