Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tour: #15 implies conversion is necessary when it isn't #7036

Closed
gopherbot opened this issue Dec 31, 2013 · 6 comments
Closed

x/tour: #15 implies conversion is necessary when it isn't #7036

gopherbot opened this issue Dec 31, 2013 · 6 comments

Comments

@gopherbot
Copy link

by blair@orcaware.com:

Going through the language tour on slide 15 ( http://tour.golang.org/#15) it states:

"""Unlike in C, in Go assignment between items of different type requires
an explicit conversion. Try removing the float64 or int conversions in the example and
see what happens."""

However, removing the float64 conversion doesn't break the script, it looks like the int
is promoted to a float64 without any issue.  Reading the text implies to me that this
line would break if it is changed from:

    var f float64 = math.Sqrt(float64(3*3 + 4*4))

to

    var f float64 = math.Sqrt(3*3 + 4*4)
@gopherbot
Copy link
Author

Comment 1 by islandberry23:

See http://golang.org/ref/spec#Constant_expressions.  The expression 3*3 + 4*4 is
untyped, not an int.

@gopherbot
Copy link
Author

Comment 2 by blair@orcaware.com:

Thanks, however, constants are first introduced on slide #16.  Being a Go newbie, I find
the text confusing and it implies that the float64() is required when it isn't, unlike
the `int(f)` which is required.

@gopherbot
Copy link
Author

Comment 3 by sieben07:

package main
import (
    "fmt"
    "math"
)
func main() {
    var x, y int = 3, 4
    var f float64 = math.Sqrt(float64(x*x + y*y))
    var z int = int(f)
    fmt.Println(x, y, z)
}
since we have x and y int = 3, 4
it would make sense:
var f float64 = math.Sqrt(float64(x*x + y*y))
Now the program and the text would fit.

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 4:

Labels changed: added repo-tour, release-go1.2.1.

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 5:

Labels changed: removed release-go1.2.1.

@adg
Copy link
Contributor

adg commented Mar 4, 2014

Comment 6:

Owner changed to @adg.

@bradfitz bradfitz removed the new label Dec 18, 2014
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title http://tour.golang.org/#15 implies conversion is necessary when it isn't x/tour: #15 implies conversion is necessary when it isn't Apr 14, 2015
@rsc rsc modified the milestones: Unreleased, Unplanned Apr 14, 2015
@rsc rsc removed the repo-tour label Apr 14, 2015
@adg adg closed this as completed May 27, 2016
@golang golang locked and limited conversation to collaborators May 27, 2017
@rsc rsc unassigned adg Jun 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants