dongzhi9032 2017-06-15 16:00
浏览 16
已采纳

导入包

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
}
  • 写回答

1条回答 默认 最新

  • dsgdfh302506 2017-06-15 16:22
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?