Because v := -1 declares a new variable and saves it in v - and the default
type of an integer constant is int. After that, this int gets converted to
a uint, which is fine.
In the case of uint(-1), you have an untyped constant, which you then try
to cast directly to a specific type (making a typed constant). But a
uint-constant can't be -1.

So, there are two differences: one is between a constant and a variable
(for example, uint(int(-1)) also doesn't work, as you are still making a
constant) and two, between casting an int to a uint and trying to create a
negative uint const.

On Mon, Feb 27, 2017 at 12:16 AM, Felix Sun <sunf...@gmail.com> wrote:

> But why these two way don't have consistent behavior ?
>
> uint(-1)
>
>
> v :=-1
> uint(v)
>
>
> Michael Jones <michael.jo...@gmail.com>于2017年2月26日 周日上午11:35写道:
>
>> yes.
>> https://en.wikipedia.org/wiki/Two's_complement
>>
>> On Sat, Feb 25, 2017 at 10:21 PM, Felix Sun <sunf...@gmail.com> wrote:
>>
>> https://play.golang.org/p/TmxMmltHGH
>>
>> package main
>>
>> import (
>> "fmt"
>> )
>>
>> func main() {
>> var f int = -1
>> fmt.Println("become huge number:", uint(f))
>> fmt.Println("this panic", uint(-1))
>>
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>>
>> --
>> Michael T. Jones
>> michael.jo...@gmail.com
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to