What does "nil implay?

172 views
Skip to first unread message

伊藤和也

unread,
Jan 20, 2019, 6:00:24 PM1/20/19
to golang-nuts
I know "nil" is zero values for slices, maps, interfaces, etc but I don't know what "nil" implays. Does nil implay the absence of value or a variable has't been initialized yet or something else? 

David Riley

unread,
Jan 20, 2019, 6:46:58 PM1/20/19
to 伊藤和也, golang-nuts
On Jan 20, 2019, at 18:00, 伊藤和也 <kazya.i...@gmail.com> wrote:

I know "nil" is zero values for slices, maps, interfaces, etc but I don't know what "nil" implays. Does nil implay the absence of value or a variable has't been initialized yet or something else?

Basically yes, all of the above. This pattern dates back to C, Lisp, and other languages of computer science history, though many languages have different semantics attached. In C, NULL was just a zero pointer value that effectively stood for an empty/uninitialized value; in Lisp (or Python, for example, which has a similar None), nil is a special value of its own type that connoted an empty value, or false (there is similarly a “t” in Lisp which connotes the opposite of nil, though it is valueless).

In C, NULL values can be dangerous to play around with; in Go, some effort has been made to make them useful (for example, they are treated an empty arrays, though they are not quite the same as an empty map).


- Dave

Inanc Gumus

unread,
Jan 21, 2019, 7:05:51 AM1/21/19
to golang-nuts
Nil value doesn't mean the absence of a value, because nil itself is a value. For example, a slice variable can hold the nil value, in that case, it means that the slice hasn't been initialized yet (that it has no backing array).

伊藤和也

unread,
Jan 21, 2019, 8:10:47 AM1/21/19
to golang-nuts


2019年1月21日月曜日 21時05分51秒 UTC+9 Inanc Gumus:

伊藤和也

unread,
Jan 21, 2019, 8:19:03 AM1/21/19
to golang-nuts
So what do you think "nil" represents instead?
"nil" is just a special value?  for slices, maps.....

2019年1月21日月曜日 8時00分24秒 UTC+9 伊藤和也:

Inanc Gumus

unread,
Jan 21, 2019, 8:28:44 AM1/21/19
to 伊藤和也, golang-nuts
Nil = Uninitialized (slices, maps, channels, funcs etc)
Nil = Doesn't point to anything (pointers)

And so on.

--
Inanc Gumus

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/6LSHq2Bc8Gk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


diego patricio

unread,
Jan 21, 2019, 12:05:06 PM1/21/19
to 伊藤和也, golang-nuts
From stackoverflow, i think that is a proper explanation

image.png


Regards

--
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...@googlegroups.com.

Inanc Gumus

unread,
Jan 21, 2019, 2:33:11 PM1/21/19
to diego patricio, 伊藤和也, golang-nuts
Actually, that's not the whole story. For example, take a look at this example: https://play.golang.org/p/Wxq8vrBoqDr

Inanc Gumus

You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/6LSHq2Bc8Gk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

Ian Lance Taylor

unread,
Jan 21, 2019, 5:21:29 PM1/21/19
to 伊藤和也, golang-nuts
On Mon, Jan 21, 2019 at 5:19 AM 伊藤和也 <kazya.i...@gmail.com> wrote:
>
> So what do you think "nil" represents instead?
> "nil" is just a special value? for slices, maps.....

There is no one answer to that question.

As you know, `nil` is the value of an uninitialized variable of
interface, slice, pointer, map, function, or channel type. Beyond
that it has no shared meaning. For each of those types, the value
`nil` behaves differently, in a way that makes sense for the type.
See also https://golang.org/issue/22729.

Ian



> 2019年1月21日月曜日 8時00分24秒 UTC+9 伊藤和也:
>>
>> I know "nil" is zero values for slices, maps, interfaces, etc but I don't know what "nil" implays. Does nil implay the absence of value or a variable has't been initialized yet or something else?
>

Victor Giordano

unread,
Jan 22, 2019, 8:17:34 AM1/22/19
to golang-nuts
As i see things... 

  • From a language specific level (higher abstraction) is means undefined, is the absence of a value.  Something whose value is nil/null/undefined is not defined.
  • On a lower level of abstraction it may be tweeking a little bit for having an application/business depedant meaning. See this example. 
    • The trick is to implement the "Stringer" interace so the fmt.Print* checking if the receiver argument is nil or not. Do note that nil may have sense in more concrete level of abstraction, that is not a new concept, it was already introduced with the null pattern, and here i see it as well (with another custome the principles behind are the same

Victor Giordano

unread,
Jan 22, 2019, 8:19:21 AM1/22/19
to golang-nuts
i just make an anwser.....

Jan Mercl

unread,
Jan 22, 2019, 8:23:36 AM1/22/19
to Victor Giordano, golang-nuts
On Tue, Jan 22, 2019 at 2:17 PM Victor Giordano <vituc...@gmail.com> wrote:

> From a language specific level (higher abstraction) is means undefined, is the absence of a value. 

No value in Go can _not_ have a value.


> Something whose value is nil/null/undefined is not defined.

All nil values are perfectly defined: they are the zero value of the particular type.

--

-j

Victor Giordano

unread,
Jan 23, 2019, 6:42:09 AM1/23/19
to golang-nuts
I was giving an interpretaion about the meaning of nil from different pespectives

Victor Giordano

unread,
Jan 23, 2019, 6:45:00 AM1/23/19
to golang-nuts


El martes, 22 de enero de 2019, 10:23:36 (UTC-3), Jan Mercl escribió:
Just to point.. .zero is value different that nil.  

--

-j

Jan Mercl

unread,
Jan 23, 2019, 7:09:12 AM1/23/19
to Victor Giordano, golang-nuts
On Wed, Jan 23, 2019 at 12:45 PM Victor Giordano <vituc...@gmail.com> wrote:

>> All nil values are perfectly defined: they are the zero value of the particular type.
>
> Just to point.. .zero is value different that nil.

Not sure what's meant by this. When is a nil value of a type different from its zero value?

--

-j

Victor Giordano

unread,
Jan 23, 2019, 7:10:01 AM1/23/19
to Jan Mercl, golang-nuts
0 != nil

Jan Mercl

unread,
Jan 23, 2019, 7:21:01 AM1/23/19
to Victor Giordano, golang-nuts
On Wed, Jan 23, 2019 at 1:09 PM Victor Giordano <vituc...@gmail.com> wrote:

> 0 != nil

I don't understand what is has to do with what I wrote. Your example is not a valid Go expression. Not all types have nil values.

Let me reiterate: A nil value of a type is always equal to the zero value of that type. Because zero value of a type is well defined, a nil value of a type is also well defined.

--

-j

Victor Giordano

unread,
Jan 23, 2019, 7:31:13 AM1/23/19
to Jan Mercl, golang-nuts
You wrote
 
All nil values are perfectly defined: they are the zero value of the particular type.  

As i see things the nil is the default value for the pointers. If you want to call it "zero value" to the default is up to, for me doesn't work like that. For me "zero" (0) is a value and "nil" is another value. 

Hope you get what i'm saying.

Jan Mercl

unread,
Jan 23, 2019, 7:51:31 AM1/23/19
to Victor Giordano, golang-nuts
No I don't, sorry.

Zero (0) is a number. Numbers (types int, intNN, float32, ...) cannot have a nil value. I don't understand how numbers got involved in this discussion about nil values. They're not related.

But some types allow nil values (chan T, []T, ...). Nil values of such types are equal to the zero value of that type. So nil values are well defined.



--

-j

Volker Dobler

unread,
Jan 23, 2019, 7:56:34 AM1/23/19
to golang-nuts
The term "zero value" in regard to Go's types and values is
not subject to discussion, personal opinion or taste but
has a defined meaning: It is a specified technical term.
Please take some minutes and look it up in the language
spec.
The term "the zero value of type X" has a defined meaning
and must not be conflated with the integer literal 0.

If you want to discuss physics you have to stick your meaning
of "energy" or "momentum" to the official technical terms if
you want to be taken serious.

V.
 
Reply all
Reply to author
Forward
0 new messages