6

I have the following structure:

/*gotime.go*/
package gotime

type Now struct {
    dnow int
    ynow int
    mnow time.Month
}

And is there a function like:

/*gotime.go*/
func (n Now) DayNow() int {
    n.dnow = time.Now().Day()
    return n.dnow

}

I'm getting the following error when I want to call this package:

/*main.go*/
package main

import (
    "fmt"
    "./gotime"
)

blah := Now
fmt.Println(blah.DayNow())

I get errors:

# command-line-arguments
.\main.go:5: imported and not used: "_/C_/Users/ali/Desktop/test/gotime"
.\main.go:10: undefined: Now

You can look at all of the package on GitHub:

Link for this package

How can I solve this problem?

0

1 Answer 1

6

Since Now is a struct, you need a struct Composite literal to create a value of that type.

Also since it is from another package, you need the Qualified name:

blan := gotime.Now{}

Notice that you have to prepend the package name: package-name.foo.

Also since you are modifying it, you should / need to use a pointer receiver:

func (n *Now) DayNow() int {
    n.dnow = time.Now().Day()
    return n.dnow
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.