godoc: Restore anchors for variables and constants

godoc once again adds anchors for variable and constant
declarations. Individual declarations and lists of declarations are
both covered.

Fixes golang/go#19894

Change-Id: If5f8f0c3429774f33535e2e654685ba5d71937f1
Reviewed-on: https://go-review.googlesource.com/40300
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go
index 0595a15..afc3137 100644
--- a/godoc/godoc_test.go
+++ b/godoc/godoc_test.go
@@ -157,6 +157,33 @@
 	}
 }
 
+// Test that we add <span id="ConstName"> elements to the HTML
+// of definitions in const and var specs.
+func TestValueSpecIDAttributes(t *testing.T) {
+	got := linkifySource(t, []byte(`
+package foo
+
+const (
+	NoDoc string = "NoDoc"
+
+	// Doc has a comment
+	Doc = "Doc"
+
+	NoVal
+)`))
+	want := `const (
+<span id="NoDoc">NoDoc</span> <a href="/pkg/builtin/#string">string</a> = &#34;NoDoc&#34;
+
+<span class="comment">// Doc has a comment</span>
+<span id="Doc">Doc</span> = &#34;Doc&#34;
+
+<span id="NoVal">NoVal</span>
+)`
+	if got != want {
+		t.Errorf("got: %s\n\nwant: %s\n", got, want)
+	}
+}
+
 func TestCompositeLitLinkFields(t *testing.T) {
 	got := linkifySource(t, []byte(`
 package foo
@@ -169,7 +196,7 @@
 	want := `type T struct {
 <span id="T.X"></span>X <a href="/pkg/builtin/#int">int</a>
 }
-var S <a href="#T">T</a> = <a href="#T">T</a>{<a href="#T.X">X</a>: 12}`
+var <span id="S">S</span> <a href="#T">T</a> = <a href="#T">T</a>{<a href="#T.X">X</a>: 12}`
 	if got != want {
 		t.Errorf("got: %s\n\nwant: %s\n", got, want)
 	}
diff --git a/godoc/linkify.go b/godoc/linkify.go
index 0897370..0272059 100644
--- a/godoc/linkify.go
+++ b/godoc/linkify.go
@@ -109,7 +109,7 @@
 			}
 		case *ast.ValueSpec:
 			for _, n := range n.Names {
-				linkMap[n] = link{mode: identVal}
+				linkMap[n] = link{mode: identVal, name: n.Name}
 			}
 		case *ast.TypeSpec:
 			linkMap[n.Name] = link{mode: identDef}