FAQ
Placing an interface inside a struct might not be the best option in every
case, but I was wondering how do I create a factory function for such a
struct? Below is my failed example.

If I remove the comments from the factory function, I get:

invalid type for composite literal: Counter


If I remove the Counter{} argument, I get:


too few values in struct initializer


If I put Counter as an argument, I get:


type Counter is not an expression


Code:

/**********************************************************************
  * Interface inside struct *
  *********************************************************************/

package main
import "fmt"


type Counter interface {
     increase()
     decrease()
     nothing() // testing
}


type Number struct {
     value int
     co Counter
}


// factory function
//func NewNumber( x int ) *Number {
// return &Number{ x, Counter{} }
//}


func (nu Number) increase() {
     a := nu.value
     a++
     fmt.Printf( "My value: %d \n", a )

     nu.value = a
     fmt.Printf( "Increasing myself: %d \n", number.value )

     //nu.value = nu.value + 1
}


func (nu Number) decrease() {
     //a := nu.value
     //a--
     //nu.value = a

     nu.value--
}


func (nu Number) nothing() {
     nu.value = nu.value
}


var (

     number Number
)


func main() {

     number.value = 0
     fmt.Printf( "Number in the beginning: %d \n", number.value )
     number.increase()
     fmt.Printf( "Increased number: %d \n", number.value )
     fmt.Println()
}

If I run this program I get:

Number in the beginning: 0

My value: 1

Increasing myself: 0

Increased number: 0



--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Justin Israel at Mar 5, 2016 at 7:44 pm
    To fix the error in your factory function, try replacing Counter{} with
    nil.
    Counter is an interface and not a concrete type, so you cannot construct
    instances of an interface like that.

    Although I am not clear why your Number struct implements your Counter
    interface, as well as wanting to store a Counter interface field. That
    aspect has no relation to your desire to have a factory function and have a
    Counter interface field on your struct.

    Justin
    On Sun, 6 Mar 2016 8:29 AM Tomi Häsä wrote:

    Placing an interface inside a struct might not be the best option in every
    case, but I was wondering how do I create a factory function for such a
    struct? Below is my failed example.

    If I remove the comments from the factory function, I get:

    invalid type for composite literal: Counter


    If I remove the Counter{} argument, I get:


    too few values in struct initializer


    If I put Counter as an argument, I get:


    type Counter is not an expression


    Code:

    /**********************************************************************
    * Interface inside struct *
    *********************************************************************/

    package main
    import "fmt"


    type Counter interface {
    increase()
    decrease()
    nothing() // testing
    }


    type Number struct {
    value int
    co Counter
    }


    // factory function
    //func NewNumber( x int ) *Number {
    // return &Number{ x, Counter{} }
    //}


    func (nu Number) increase() {
    a := nu.value
    a++
    fmt.Printf( "My value: %d \n", a )

    nu.value = a
    fmt.Printf( "Increasing myself: %d \n", number.value )

    //nu.value = nu.value + 1
    }


    func (nu Number) decrease() {
    //a := nu.value
    //a--
    //nu.value = a

    nu.value--
    }


    func (nu Number) nothing() {
    nu.value = nu.value
    }


    var (

    number Number
    )


    func main() {

    number.value = 0
    fmt.Printf( "Number in the beginning: %d \n", number.value )
    number.increase()
    fmt.Printf( "Increased number: %d \n", number.value )
    fmt.Println()
    }

    If I run this program I get:

    Number in the beginning: 0

    My value: 1

    Increasing myself: 0

    Increased number: 0



    --
    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+unsubscribe@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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nico at Mar 6, 2016 at 7:17 am

    On 05/03/16 19:29, Tomi Häsä wrote:
    Placing an interface inside a struct might not be the best option in every case, but I was wondering how do I create a factory function for such a struct? Below is my failed example.
    Just, use nil (see the updated code below).

    ---

    I would also read this FAQ entry https://golang.org/doc/faq#methods_on_values_or_pointers .

    ---

    https://play.golang.org/p/CId4GQWg6u

    package main

    import "fmt"

    type Counter interface {
      increase()
      decrease()
      nothing() // testing
    }

    type Number struct {
      value int
      co Counter
    }

    func NewNumber(value int) *Number {
      return &Number{value, nil}
    }

    func (nu *Number) increase() {
      a := nu.value
      a++
      fmt.Printf("My value: %d \n", a)

      nu.value = a
      fmt.Printf("Increasing myself: %d \n", number.value)

      nu.value = nu.value + 1
    }

    func (nu *Number) decrease() {
      nu.value--
    }

    func (nu *Number) nothing() {
      nu.value = nu.value
    }

    var (
      number Number
    )

    func main() {

      number.value = 0
      fmt.Printf("Number in the beginning: %d \n", number.value)
      number.increase()
      fmt.Printf("Increased number: %d \n", number.value)
      fmt.Println()
    }

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Krzysztof Kowalczyk at Mar 7, 2016 at 4:47 am
    To reiterate what Justin said, embedding Counter inside Number does nothing
    in this particular example and probably isn't what you meant.

    Type (Number) conforms to interface (Counter) by implementing interface
    methods. What you probably meant is just this:

    https://play.golang.org/p/GMwaZ7Lg6S

    package main

    import "fmt"

    type Counter interface {
    increase()
    decrease()
    nothing() // testing
    }

    type Number struct {
    value int
    }

    func NewNumber(value int) *Number {
    return &Number{value}
    }

    func (nu *Number) increase() {
    a := nu.value
    a++
    fmt.Printf("My value: %d \n", a)

    nu.value = a
    fmt.Printf("Increasing myself: %d \n", number.value)

    nu.value = nu.value + 1
    }

    func (nu *Number) decrease() {
    nu.value--
    }

    func (nu *Number) nothing() {
    nu.value = nu.value
    }

    var (
    number Number
    )

    func main() {
    number.value = 0
    fmt.Printf("Number in the beginning: %d \n", number.value)
    number.increase()
    fmt.Printf("Increased number: %d \n", number.value)
    fmt.Println()
    }

    On Saturday, March 5, 2016 at 11:17:41 PM UTC-8, Nico wrote:
    On 05/03/16 19:29, Tomi Häsä wrote:
    Placing an interface inside a struct might not be the best option in
    every case, but I was wondering how do I create a factory function for such
    a struct? Below is my failed example.

    Just, use nil (see the updated code below).

    ---

    I would also read this FAQ entry
    https://golang.org/doc/faq#methods_on_values_or_pointers .

    ---

    https://play.golang.org/p/CId4GQWg6u

    package main

    import "fmt"

    type Counter interface {
    increase()
    decrease()
    nothing() // testing
    }

    type Number struct {
    value int
    co Counter
    }

    func NewNumber(value int) *Number {
    return &Number{value, nil}
    }

    func (nu *Number) increase() {
    a := nu.value
    a++
    fmt.Printf("My value: %d \n", a)

    nu.value = a
    fmt.Printf("Increasing myself: %d \n", number.value)

    nu.value = nu.value + 1
    }

    func (nu *Number) decrease() {
    nu.value--
    }

    func (nu *Number) nothing() {
    nu.value = nu.value
    }

    var (
    number Number
    )

    func main() {

    number.value = 0
    fmt.Printf("Number in the beginning: %d \n", number.value)
    number.increase()
    fmt.Printf("Increased number: %d \n", number.value)
    fmt.Println()
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 5, '16 at 7:29p
activeMar 7, '16 at 4:47a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase