function Undefined in go

github logo ・1 min read

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

twitter logo DISCUSS (9)
markdown guide
 

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)

}
 

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.

 

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

 

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

 

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.

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.

 

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

 

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

Classic DEV Post from Feb 14

GitHub draft PRs are here!

Photo by EJ Yao on Unsplash Gone or soon to be gone are the days of creating a...

debbiswas-Git profile image
I like to learn new languages and their applications ..then of course develop something out of it