sksdutra
sksdutra

Reputation: 41

Importing Packages

I'm trying to learn Go and I'm writing some test programs, but I'm having a problem when importing my packages. I have this directory tree in my Go Workspace (inside src directory) ($GOPATH):

$GOPATH/src/gogoboy
│   main.go
│
├───cpu
│       flags.go
│       instructions.go
│       registers.go
│
└───memory
        memories.go

As you can see I've 2 packages at this moment: cpu and memory and this is my main.go:

package main

import (
    "gogoboy/cpu"
    "gogoboy/memory"
)

func main() {
    cpu.InitializeRegisters()
    memory.WriteRAM(0x00, 0xFF)
}

The problem is: The package cpu, at the same level of package memory, is imported correctly and I can use every cpu function but, the package memory raises the error:

.\main.go:10: undefined: memory.WriteRAM

I really can't understand what's happening, can anyone give a way to solve?

File memory/memories.go

package memory

const size uint16 = 0x2000

type Memories struct {
    RAM  [size]uint8
    VRAM [size]uint8
}

func (memory *Memories) WriteRAM(position uint16, value uint8) {
    memory.RAM[position] = value
}

Upvotes: 2

Views: 134

Answers (1)

william.taylor.09
william.taylor.09

Reputation: 2215

You're not doing what you think you're doing; it looks like you WANT to call a method on a memory struct, but the compiler is looking for a function named WriteRam within the memory package because of how you're calling that method.

Look at your signature in memory.go:

func (memory *Memories) WriteRAM(position uint16, value uint8)

You have a receiver func (memory *Memories). This means that in order to call this method, you need to have a memory.Memories variable declared somewhere.

I think you might want your main to look like this:

package main

import (
    "gogoboy/cpu"
    "gogoboy/memory"
)

func main() {
    cpu.InitializeRegisters()
    mem := memory.Memories{}
    mem.WriteRAM(0x00, 0xFF)
}

Upvotes: 6

Related Questions