currently working with the vishvananda/netns
package trying to extract routes from a specific network namespace.
There is a defined Handle
struct which is returned when I request a 'handle' for a specific network namespace. As such:
func NewHandleAt(ns netns.NsHandle, nlFamilies ...int) (*Handle, error)
This is then a receiver argument (?) to a function that requires that handle,
func (h *Handle) LinkList() ([]Link, error)
I'm new to go and not sure how to tie these together. I'm stuck with:
func (h *Handle) showInts() {
int, err := h.netlink.LinkList()
if err != nil {
log.Fatal(err)
}
for i, r := range int {
log.Printf("%d: %s", i, r.Attrs().Name)
}
}
func main() {
ints, err := netlink.LinkList()
if err != nil {
log.Fatal(err)
}
for i, r := range ints {
log.Printf("%d: %s", i, r.Attrs().Name)
}
pid, err := netns.GetFromPid(9097)
if err != nil {
log.Fatal(err)
}
netlink.NewHandleAt(pid)
showInts()
}
Update
While writing the original answer, touched on a number of things, without any clear structure, so here's a more structured version:
Depending on what you're actually asking (ie "How do I add a receiver function/method to an exported type", or "What the hell is a receiver function"), the answers are as follows:
How do I add a receiver function to an exported type?
Easy, same as you do with any other type. You were close, in fact. This doesn't work:
Because you're adding a method to the
Handler
type in your package. Given you have amain
function, that would be themain
package. You're trying to add it to thenetlink.Handler
type instead. In which case, this will work:The type is
netlink.Handler
in your main package after all... This, however will not work. The compiler will refuse to compile, telling you: "Cannot define new methods on non-local type". This is easily mitigated, though, by creating a new type, and add the method there:Be that as it may, the last 2 lines in your code strike me as wrong. Given that
NewHandleAt
returns(*Handle, error)
, andnetlink.Handle
is a receiver argument, the correct way would be:The fact that you've "wrapped" the external type in a custom type does mean you'll find yourself casting the same thing quite a lot. Say
netlink.Handle
has aTest
method, and you want to call it insideshowInts
:I'd also change the varname from
pid
tonsh
or something, because it's aNsHandle
, and not apid
after all...What is a receiver argument?
Because you wrote this:
I get the impression you're not entirely clear on what a receiver argument is. Put simply, it's like a function argument, but instead of an argument that is just passed to a function, it's an argument that holds the object/value on which the function is called. Basically, it's the "instance" on which the function/method is called. Think of it as the
this
keyword in many OOP languages:In something like C++ would be
There are significant differences, however:
There's quite a few differences, perhaps consider going through the golang tour. The stuff about go methods can be found here
Other issues/weird things
After looking at your code again, I'm really not sure whether this is correct:
In your
main
function, you callnetlink.LinkList()
.h
is a*netlink.Handler
. If you need to call thenetlink.LinkList
function, it's highly likelyh.netlink.LinkList
is not what you want to do. Instead, you should simply callnetlink.LinkList()
.That's assuming you need to call the function in the first place.
Given that you've already called it in the
main
function, why not pass it as an argument?