Skip to content

Commit

Permalink
spec: ignore struct tags when converting structs
Browse files Browse the repository at this point in the history
This is a backwards-compatible language change.

Per the proposal (#16085), the rules for conversions are relaxed
such that struct tags in any of the structs involved in the conversion
are ignored (recursively).

Because this is loosening the existing rules, code that compiled so
far will continue to compile.

For #16085.
Fixes #6858.

Change-Id: I0feef651582db5f23046a2331fc3f179ae577c45
Reviewed-on: https://go-review.googlesource.com/24190
Reviewed-by: Russ Cox <rsc@golang.org>
  • Loading branch information
griesemer committed Oct 4, 2016
1 parent 8c24bff commit 5c7a005
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions doc/go_spec.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of September 1, 2016",
"Subtitle": "Version of October 4, 2016",
"Path": "/ref/spec"
}-->

Expand Down Expand Up @@ -3837,10 +3837,12 @@ <h3 id="Conversions">Conversions</h3>
to <code>T</code>.
</li>
<li>
<code>x</code>'s type and <code>T</code> have identical
ignoring struct tags (see below),
<code>x</code>'s type and <code>T</code> have <a href="#Type_identity">identical</a>
<a href="#Types">underlying types</a>.
</li>
<li>
ignoring struct tags (see below),
<code>x</code>'s type and <code>T</code> are unnamed pointer types
and their pointer base types have identical underlying types.
</li>
Expand All @@ -3860,6 +3862,31 @@ <h3 id="Conversions">Conversions</h3>
</li>
</ul>

<p>
<a href="#Struct_types">Struct tags</a> are ignored when comparing struct types
for identity for the purpose of conversion:
</p>

<pre>
type Person struct {
Name string
Address *struct {
Street string
City string
}
}

var data *struct {
Name string `json:"name"`
Address *struct {
Street string `json:"street"`
City string `json:"city"`
} `json:"address"`
}

var person = (*Person)(data) // ignoring tags, the underlying types are identical
</pre>

<p>
Specific rules apply to (non-constant) conversions between numeric types or
to and from a string type.
Expand Down

0 comments on commit 5c7a005

Please sign in to comment.