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

Simpler "custom unit" example? #16

Open
dmoles opened this issue Jan 29, 2019 · 4 comments
Open

Simpler "custom unit" example? #16

dmoles opened this issue Jan 29, 2019 · 4 comments

Comments

@dmoles
Copy link
Contributor

dmoles commented Jan 29, 2019

It's not quite as clean as it would be in some languages since Go doesn't let you attach extension methods to existing types, but this seems easier than the int type + cast example:

const Parsec = 648000 * unit.AstronomicalUnit / math.Pi

func ToParsecs(l unit.Length) unit.Length {
	return l/Parsec
}

// could also be:
//
// func ToParsecs(l unit.Length) float64 {
// 	return float64(l/Parsec)
// }

func main() {
	fmt.Printf("1 pc = %f ly\n", Parsec.LightYears())
	fmt.Printf("1 ly = %f pc\n", ToParsecs(unit.LightYear))
}

Prints:

1 pc = 3.261564 ly
1 ly = 0.306601 pc

Is there an advantage to the type MyUnit int (or in this case I suppose type Parsec float64) approach over this?

@martinlindhe
Copy link
Owner

No advantage, its just a trivial usage example.

Feel free to submit a patch to update the readme with more examples if you want.

@soumya92
Copy link
Collaborator

soumya92 commented Jan 29, 2019

@dmoles if you send a patch, I would also recommend adding a number, just for a more illustrative example, something like

func main() {
	fmt.Printf("8 pc = %f ly\n", (8 * Parsec).LightYears())
	fmt.Printf("4 ly = %f pc\n", ToParsecs(4 * unit.LightYear))
}

@martinlindhe
Copy link
Owner

I'm not changing anything right now.

Patches are welcome.

@dmoles
Copy link
Contributor Author

dmoles commented Feb 1, 2019

Thanks. I'll submit a patch after I've played around a bit more with the library and have confidence in my example.

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

3 participants