DEV Community

debbiswas
debbiswas

Posted on

function Undefined in go

Can any one help me why is the below code giving undefined:register error
I am trying to understand the call by reference and call by value.

package main

import (
"fmt"
)

type MyStruct struct {
name string
id int64
}

var (
s MyStruct
u int
)

func (u MyStruct) register() {
fmt.Println(u.name, u.id)
fmt.Println(u.name, u.id)
fmt.Println(u)
s.name = "vij"
fmt.Println(u)

}

func main() {

s := MyStruct{"deb", 11273090}
r := register()
fmt.Print("Inside Main****")
fmt.Print(s)

}

./prog.go:29:7: undefined: register

Top comments (9)

Collapse
 
5anthosh profile image
Santhosh Kumar

You can use markdown syntax to present the code better

Example

package main

import (
"fmt"
)

type MyStruct struct {
name string
id int64
}

var (
s MyStruct
u int
)

func (u MyStruct) register() {
fmt.Println(u.name, u.id)
fmt.Println(u.name, u.id)
fmt.Println(u)
s.name = "vij"
fmt.Println(u)

}

func main() {

s := MyStruct{"deb", 11273090}
r := register()
fmt.Print("Inside Main****")
fmt.Print(s)

}
Collapse
 
debbiswasgit profile image
debbiswas

Hi Santhosh, thanks for the suggestion. The thing is that when i format the code written in [I'm an inline link]play.golang.org/ it automatically indents the body.

Collapse
 
epavanello profile image
Emanuele Pavanello

Register is a method.
You need to call it by s.register()
And register doesn’t return anything.

Collapse
 
debbiswasgit profile image
debbiswas

Many many thanks Emanuele it helped...Sorry guys for a simple question.. please bear with me am a Novice..:(

Collapse
 
devdrake0 profile image
Si

Can you improve the title of your question please. If not done in 24 hours, the help tag will be removed.

Collapse
 
debbiswasgit profile image
debbiswas

Sorry if that was harsh ...Is this better Si

Collapse
 
devdrake0 profile image
Si • Edited

Thanks for revising it, but not much better I'm afraid. The title should be a summary of the issue, for example, for this question, it could be "function undefined in Go".

It just gives people more context into the issue without having to click on the question 😊

Edit: also no need to apologise, phrasing questions is hard. It's something you'll learn with experience.

Thread Thread
 
debbiswasgit profile image
debbiswas

Thank you for your suggestions Si.

Thread Thread
 
devdrake0 profile image
Si

Sure :) keep coming back if you need help.

I've been working with Go for a few years, so ping me if you need to.