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

tour: [The expression T(v) converts the value v to the type T.] #1624

Open
mayank5464942 opened this issue Jun 29, 2024 · 1 comment
Open

Comments

@mayank5464942
Copy link

Context: https://go.dev/tour/basics/13

"The expression T(v) converts the value v to the type T."

The above statement wrongly interprets the expression.
should'nt it be meant more as if
var a float64 = 5.3;
var b int32=int32(a);
now here the value of "a" does not change to int32 instead the assigned variable b gets the change in datatype value of a.

@FixTestRepeat
Copy link

this statement :

"The expression T(v) converts the value v to the type T."

is already correct as it is in my view.

What I found confusing is the code example, which doesn't appear to relate to this concept at all.

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 uint = uint(f)
	fmt.Println(x, y, z)
}

Instead, I would suggest a much more trivial code alternative demonstrates the effect more clearly by converting from float64 to int (and the loss of precision that causes)

package main

import (
	"fmt"
)

func main() {
	var x1, x2 float64 = 1.123, 2.345
	var y1, y2 int = int(x1), int(x2)  // Key Concept 
	
	fmt.Println(x1, x2)
	fmt.Println(y1, y2)
}

// returns:
// 1.123 2.345
// 1 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants