FAQ
Hi,

I'm new to the Go language, and am having trouble implementing
constructor-like behavior. Let's say I want to implement a type like
NDPoint, which is a splice with N elements (each element of the splice is
one of the coordinates of the point). However, I don't know at compile time
how many dimensions. I would like to have something like

func main() {
myPoint := NDPoint.NewPoint(5)
fmt.Println(myPoint)
}

NDPoint.NewPoint(5) should return a splice with 5 elements. To make this
work does NDPoint have to be its own package? If I want to disable the
.Append function of a splice, is there a way to do that (to disallow one of
the points from being N+1 dimensional)?

Thank you very much

--

Search Discussions

  • Francesc Campoy Flores at Sep 25, 2012 at 11:46 pm
    Hi,

    If you hide your slice in a struct, using a non exported field (lowercase
    first letter) you can hide the fact that there's a slice behind it.

    http://play.golang.org/p/nGZF4fmC1D

    If FixedSize was used from outside the package where it's defined,
    FixedSize.d wouldn't be accessible, therefore it would be impossible to
    append anything on it.

    Cheers,
    Francesc
    On Tue, Sep 25, 2012 at 3:44 PM, wrote:

    Hi,

    I'm new to the Go language, and am having trouble implementing
    constructor-like behavior. Let's say I want to implement a type like
    NDPoint, which is a splice with N elements (each element of the splice is
    one of the coordinates of the point). However, I don't know at compile time
    how many dimensions. I would like to have something like

    func main() {
    myPoint := NDPoint.NewPoint(5)
    fmt.Println(myPoint)
    }

    NDPoint.NewPoint(5) should return a splice with 5 elements. To make this
    work does NDPoint have to be its own package? If I want to disable the
    .Append function of a splice, is there a way to do that (to disallow one of
    the points from being N+1 dimensional)?

    Thank you very much

    --



    --
    --
    Francesc

    --
  • Tracey Brendan at Sep 26, 2012 at 12:56 am
    Thank you for your reply

    A similar idea which I have found works (at least for initialization) is:

    http://play.golang.org/p/FKO7RO4XuV

    I'm confused as to what's happening in my code, and what's happening in
    your line:

    func NewFixedSize(s int) FixedSize {
    return FixedSize{make([]int, s)}
    }

    I don't quite understand how the function is returning a seemingly
    different value than is being generated. For example, in my code, m is a
    slice, but is being returned as a DataPoint. In your code, I don't
    understand what's going on in FixedSize{make(...)}. Could you direct me to
    the right place to understand that?

    Thank you

    On Tuesday, September 25, 2012 4:46:30 PM UTC-7, Francesc Campoy Flores
    wrote:
    Hi,

    If you hide your slice in a struct, using a non exported field (lowercase
    first letter) you can hide the fact that there's a slice behind it.

    http://play.golang.org/p/nGZF4fmC1D

    If FixedSize was used from outside the package where it's defined,
    FixedSize.d wouldn't be accessible, therefore it would be impossible to
    append anything on it.

    Cheers,
    Francesc

    On Tue, Sep 25, 2012 at 3:44 PM, <tracey....@gmail.com <javascript:>>wrote:
    Hi,

    I'm new to the Go language, and am having trouble implementing
    constructor-like behavior. Let's say I want to implement a type like
    NDPoint, which is a splice with N elements (each element of the splice is
    one of the coordinates of the point). However, I don't know at compile time
    how many dimensions. I would like to have something like

    func main() {
    myPoint := NDPoint.NewPoint(5)
    fmt.Println(myPoint)
    }

    NDPoint.NewPoint(5) should return a splice with 5 elements. To make this
    work does NDPoint have to be its own package? If I want to disable the
    .Append function of a splice, is there a way to do that (to disallow one of
    the points from being N+1 dimensional)?

    Thank you very much

    --



    --
    --
    Francesc
    --
  • Dan Kortschak at Sep 26, 2012 at 1:16 am
    The function is returning a struct with the d field value being set to
    the return of make([]int, s):

    func NewFixedSize(s int) FixedSize {
    return FixedSize{
    d: make([]int, s),
    }
    }

    On Tue, 2012-09-25 at 17:56 -0700, tracey.brendan@gmail.com wrote:
    I don't quite understand how the function is returning a seemingly
    different value than is being generated. For example, in my code, m is
    a
    slice, but is being returned as a DataPoint. In your code, I don't
    understand what's going on in FixedSize{make(...)}. Could you direct
    me to
    the right place to understand that?

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 25, '12 at 11:29p
activeSep 26, '12 at 1:16a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase