Need help to understand structure initialise

Hi Team ,

I am new to golang , I have writter a small script refrencing function and structure in another file . I am able to run the script by default structure initialize

Main script

package main

import (
“fmt”
“local/find”
)

func main() {
d:=find.Sam{}

for i:=0;i<100;i++ {
	if find.Odd(i) {
		fmt.Println("even number",i)
	} else {
		fmt.Println("odd number",i)
	}
	fmt.Println("Finding the number",d.Add(i,2))
}

}

--------package

package find

type Sam struct {
j int
}

func Odd(a int) bool {
if a%2 == 0 {
return true
} else {
return false
}
}

func (d *Sam)Add(a, b int) int {
d.j=a + b + d.j
return d.j
}

I want to initialize j to 100 , right now it getting error if initialize to 10

Found the solution , the issue is in declaration , it suppose to be in capital
package find

type Sam struct {
J int
}

func Odd(a int) bool {
if a%2 == 0 {
return true
} else {
return false
}
}

func (d *Sam)Add(a, b int) int {
d.J=a + b + d.J
return d.J
}


package main

import (
“fmt”
“local/find”
)

func main() {
d:=find.Sam{J:300}
for i:=0;i<100;i++ {
if find.Odd(i) {
fmt.Println(“even number”,i)
} else {
fmt.Println(“odd number”,i)
}
fmt.Println(“Finding the number”,d.Add(i,2))
}
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.