FAQ
Hello gophers,


I ran into the following error today: trying to write an untyped constant
to a bytes buffer fails, although a straight comparison to a non int type
works fine:
http://play.golang.org/p/0FvQ49doC0

I did not expect this at first and but now I think I understand why that is
the case.

But isn't the behavior inconsistent??

--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rob Pike at Jan 8, 2015 at 9:45 pm
    As the documentation for binary.Write says, the data must be a fixed-size
    value. Ints are not fixed-size (int32s are), and the language rules mean
    that the call passes c as an int-typed value (see
    http://blog.golang.org/constants).

    Nothing inconsistent here, although it is subtle.

    -rob

    On Fri, Jan 9, 2015 at 8:37 AM, wrote:

    Hello gophers,


    I ran into the following error today: trying to write an untyped constant
    to a bytes buffer fails, although a straight comparison to a non int type
    works fine:
    http://play.golang.org/p/0FvQ49doC0

    I did not expect this at first and but now I think I understand why that
    is the case.

    But isn't the behavior inconsistent??

    --
    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 [email protected].
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Axel Wagner at Jan 8, 2015 at 9:54 pm
    Hi,

    [email protected] writes:
    I did not expect this at first and but now I think I understand why that is
    the case.

    But isn't the behavior inconsistent??
    No. The Spec [1] can shed some light on this. c is an untyped integer
    constant. As such, it has a default type of int. In both your cases, it
    will be given a type implicitely when used as an operand in an
    expression. In the first case, the expression fixes the type to be
    uint32, so it will be given this type *in this expression*. In the
    second expression, it is passed as an interface{}, which has no explicit
    type, so it reverts to its default type, which is int.

    This is "consistent behaviour of an untyped constant", as defined by the spec
    (which you accept by writing const c = 0xCAFE in the first
    place. Without untyped constants and their definition, that would be an
    error).
    If you want a typed constant, just do const c = uint32(0xCAFE), then you
    get "consistent behaviour with a typed constant" (which you might phrase
    as just "constistent").

    Best,

    Axel

    [1] http://golang.org/ref/spec#Constants

    --
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 8, '15 at 9:37p
activeJan 8, '15 at 9:54p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase