FAQ
hi i am new at go language.

here is my question

package main
import "fmt"
//define a Rectangle struct that has a length and a width

type Rectangle struct { length, width int } //write a function Area that
can apply to a Rectangle type
func (r Rectangle) Area() int {
     return r.length * r.width
}

func main() {
     r := Rectangle{length:5, width:3} //define a new Rectangle instance
with values for its properties
     fmt.Println("Rectangle details are: ",r)
     fmt.Println("Rectangle's area is: ", r.Area()) <--- "HERE"
}

what i know is when i call a function with value
then
fmt.Println("Rectangle's area is: ", r.Area())< --- "HERE"
will form change like this
fmt.Println("Rectangle's area is: ", Area(r))

isn't it?

sorry for my poor english
i wish understand my question.


--
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/groups/opt_out.

Search Discussions

  • Zephyrtronium at Jun 17, 2013 at 3:44 pm
    To be more precise, r.Area() is equivalent to Rectangle.Area(r), or if Area
    is defined on a pointer receiver and r is still a value, then it is the
    same as Rectangle.Area(&r).
    On Monday, June 17, 2013 9:43:23 AM UTC-4, rhl fod wrote:

    what i know is when i call a function with value
    then
    fmt.Println("Rectangle's area is: ", r.Area())< --- "HERE"
    will form change like this
    fmt.Println("Rectangle's area is: ", Area(r))
    --
    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/groups/opt_out.
  • Ian Lance Taylor at Jun 17, 2013 at 3:44 pm

    On Mon, Jun 17, 2013 at 6:43 AM, rhl fod wrote:
    func main() {
    r := Rectangle{length:5, width:3} //define a new Rectangle instance with
    values for its properties
    fmt.Println("Rectangle details are: ",r)
    fmt.Println("Rectangle's area is: ", r.Area()) <--- "HERE"
    }

    what i know is when i call a function with value
    then
    fmt.Println("Rectangle's area is: ", r.Area())< --- "HERE"
    will form change like this
    fmt.Println("Rectangle's area is: ", Area(r))

    isn't it?
    I'm sorry, I don't understand the question.

    If you are asking: can you write Area(r) instead of r.Area(), the answer is no.

    If you are asking: is the efficiency of r.Area() the same as the
    efficiency of Area(r), the answer is yes.

    Ian

    --
    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/groups/opt_out.
  • Minux at Jun 17, 2013 at 3:44 pm

    On Mon, Jun 17, 2013 at 9:43 PM, rhl fod wrote:
    hi i am new at go language.

    here is my question

    package main
    import "fmt"
    //define a Rectangle struct that has a length and a width

    type Rectangle struct { length, width int } //write a function Area that can
    apply to a Rectangle type
    func (r Rectangle) Area() int {
    return r.length * r.width
    }

    func main() {
    r := Rectangle{length:5, width:3} //define a new Rectangle instance with
    values for its properties
    fmt.Println("Rectangle details are: ",r)
    fmt.Println("Rectangle's area is: ", r.Area()) <--- "HERE"
    }

    what i know is when i call a function with value
    then
    fmt.Println("Rectangle's area is: ", r.Area())< --- "HERE"
    will form change like this
    fmt.Println("Rectangle's area is: ", Area(r))
    i guess you want a func that takes a Rectangle parameter?

    fmt.Println("Rectangle's area is:", Rectangle.Area(r))

    --
    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/groups/opt_out.
  • peterGo at Jun 17, 2013 at 3:59 pm
    rhl fod,

    The Go Programming Language Specification
    http://golang.org/ref/spec

    Method declarations
    http://golang.org/ref/spec#Method_declarations

    Given type Point, the declarations

    func (p *Point) Length() float64 {
         return math.Sqrt(p.x * p.x + p.y * p.y)
    }

    func (p *Point) Scale(factor float64) {
         p.x *= factor
         p.y *= factor
    }

    bind the methods Length and Scale, with receiver type *Point, to the base
    type Point.

    The type of a method is the type of a function with the receiver as first
    argument. For instance, the method Scale has type

    func(p *Point, factor float64)

    However, a function declared this way is not a method.

    Peter
    On Monday, June 17, 2013 9:43:23 AM UTC-4, rhl fod wrote:

    hi i am new at go language.

    here is my question

    package main
    import "fmt"
    //define a Rectangle struct that has a length and a width

    type Rectangle struct { length, width int } //write a function Area that
    can apply to a Rectangle type
    func (r Rectangle) Area() int {
    return r.length * r.width
    }

    func main() {
    r := Rectangle{length:5, width:3} //define a new Rectangle instance
    with values for its properties
    fmt.Println("Rectangle details are: ",r)
    fmt.Println("Rectangle's area is: ", r.Area()) <--- "HERE"
    }

    what i know is when i call a function with value
    then
    fmt.Println("Rectangle's area is: ", r.Area())< --- "HERE"
    will form change like this
    fmt.Println("Rectangle's area is: ", Area(r))

    isn't it?

    sorry for my poor english
    i wish understand my question.
    --
    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/groups/opt_out.
  • Rhl fod at Jun 17, 2013 at 7:23 pm
    I thought this question is silly until now.
    Thank you so much for your help (all of you)

    Dear iant
    I didn't know about r.Area() equal Rectangle.Area(r)).

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 17, '13 at 3:13p
activeJun 17, '13 at 7:23p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase