[go-nuts] Unset a variable

12,664 views
Skip to first unread message

kar

unread,
May 13, 2010, 1:47:40 AM5/13/10
to golang-nuts
is there a way to unset dynamic variable immediately, something
similar to unset?

chris dollin

unread,
May 13, 2010, 2:08:10 AM5/13/10
to kar, golang-nuts
On 13 May 2010 06:47, kar <akma...@gmail.com> wrote:
is there a way to unset dynamic variable immediately, something
similar to unset?

Are you sure you're in the right group? If you are, could
you clarify what you mean in Go by a "dynamic variable"?

Chris

--
Chris "allusive" Dollin

Andrew Gerrand

unread,
May 13, 2010, 2:33:18 AM5/13/10
to kar, golang-nuts
There is no such thing as a "dynamic variable" in Go.

Once you declare a variable, it has a value (its type's zero value,
unless otherwise specified).

You can, however, structure your code such that variables you're no
longer using fall out of scope when you're done with them.

In the following trivial example, the second time d is declared it
actually creates a new variable, which effectively disappears after
the inner block closes.

If you were to remove the first variable declaration, the program
wouldn't compile as the second println statement would refer to a d
that doesn't exist there.

--

package main

func main() {
d := 1
{
d := 2
println(d)
}
println(d)
}

--

Hope this helps!
Andrew

kar

unread,
May 13, 2010, 8:52:34 AM5/13/10
to golang-nuts
What i meant was, i 've created a simple pkg for fifo queue, as below

type node_ struct {
value string
prev *node_
next *node_
}

type FifoString_ struct {
first *node_
last *node_
Count uint64
}

func (o *FifoString_) Push(s string) {
var n *node_
n = new(node_)
if n == nil { return }
n.value = s

if (o.Count == 0) {
o.first = n
o.last = n
} else {
n.prev = o.last
o.last.next = n
o.last = n
}
o.Count++
if n == nil { return }
}

func (o *FifoString_) Pop() (o1 string, o2 bool) {
if o.Count == 0 { return "", false }
o.Count--
s := o.first.value
if o.Count == 0 {
o.first = nil
} else {
o.first = o.first.next
}
return s, true
}

so when i null out the var on Pop() method, im leaving it to GC to
clear it, is there a way to manually clear them?

On May 13, 2:33 pm, Andrew Gerrand <a...@golang.org> wrote:
> There is no such thing as a "dynamic variable" in Go.
>
> Once you declare a variable, it has a value (its type's zero value,
> unless otherwise specified).
>
> You can, however, structure your code such that variables you're no
> longer using fall out of scope when you're done with them.
>
> In the following trivial example, the second time d is declared it
> actually creates a new variable, which effectively disappears after
> the inner block closes.
>
> If you were to remove the first variable declaration, the program
> wouldn't compile as the second println statement would refer to a d
> that doesn't exist there.
>
> --
>
> package main
>
> func main() {
>         d := 1
>         {
>                 d := 2
>                 println(d)
>         }
>         println(d)
>
> }
>
> --
>
> Hope this helps!
> Andrew
>

chris dollin

unread,
May 13, 2010, 9:20:12 AM5/13/10
to kar, golang-nuts
On 13 May 2010 13:52, kar <akma...@gmail.com> wrote:
What i meant was, i 've created a simple pkg for fifo queue, as below

type node_ struct {
       value string
       prev *node_
       next *node_

(Wyy those trailing _s?)

func (o *FifoString_) Push(s string) {
       var n *node_
       n = new(node_)
       if n == nil { return }

n will never be nil; you just new'd it. In fact you can just go

    n := new(node_)
 
       n.value = s

Or
    n := &node_{value: s}
 
       if n == nil { return }

n will never be nil. Even if it could be, you return anyway
(at the end of the function), so this code is unnecessary.
 
func (o *FifoString_) Pop() (o1 string, o2 bool) {
       if o.Count == 0 { return "", false }
       o.Count--
       s := o.first.value
       if o.Count == 0 {
               o.first = nil
       } else {
               o.first = o.first.next
       }
       return s, true
}

so when i null out the var on Pop() method, im leaving it to GC to
clear it, is there a way to manually clear them?

Clear what? o.first is as clear as a variable can get.

--
Chris "allusive" Dollin

kar

unread,
May 13, 2010, 9:35:07 AM5/13/10
to golang-nuts
thanks for the tips. got it.

On May 13, 9:20 pm, chris dollin <ehog.he...@googlemail.com> wrote:
Reply all
Reply to author
Forward
0 new messages