Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:11:55+00:00 2026-06-14T08:11:55+00:00

Given the setup in the 54th slide of the golang tour: type Abser interface

  • 0

Given the setup in the 54th slide of the golang tour:

type Abser interface {
    Abs() float64
}

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

Why can’t a method also be defined for the struct as well as the pointer to the struct? That is:

func (v Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

Defining this gives the following error:

prog.go:41: method redeclared: Vertex.Abs
    method(*Vertex) func() float64
    method(Vertex) func() float64
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:11:56+00:00Added an answer on June 14, 2026 at 8:11 am

    It can. Just define it on the struct and not the pointer. It will resolve both ways

    Method Sets

    The method set of the corresponding pointer type *T is the set of all
    methods with receiver *T or T (that is, it also contains the method
    set of T)

    Try live: http://play.golang.org/p/PsNUerVyqp

    package main
    
    import (
        "fmt"
        "math"
        )
    
    type Abser interface {
        Abs() float64
    }
    
    type Vertex struct {
        X, Y float64
    }
    
    func (v Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
    }
    
    func main() {
        v := Vertex{5, 10}
        v_ptr := &v
        fmt.Println(v.Abs())
        fmt.Println(v_ptr.Abs())
    }
    

    Update: As per comments I have created an extra example that actually makes use of the Abser interface to illustrate that both the value and the pointer satisfy the interface.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup Given this user-defined type: struct T { static int x; int y; T()
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
Given the below setup and code snippets, what reasons can you come up with
I am running into trouble given the following setup: Operating System is Windows 7
Given this : delimiter // create procedure setup() begin declare d datetime; set d
Given a list of arrays and lots of setup time I need to quickly
Given an hypothetical utility class that is used only in program setup: class MyUtils
Given multi-tenant application, How are vendors implementing email notifications from an email account setup
Given the following (demonstration) project layout: MyProject/ README LICENSE setup.py myproject/ ... # packages
From the given setup IEnumerable<int> one = new int[] { 1, 2, 3, 4,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.