如何设置golaang项目?

问题描述 投票:0回答:1

我有这个代码:

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"

func main() {
bleAdaptor := ble.NewClientAdaptor(os.Args[1])
battery := ble.NewBatteryDriver(bleAdaptor)

work := func() {
    gobot.Every(5*time.Second, func() {
        fmt.Println("Battery level:", battery.GetBatteryLevel())
    })
}

robot := gobot.NewRobot("bleBot",
    []gobot.Connection{bleAdaptor},
    []gobot.Device{battery},
    work,
)

robot.Start()
}

我运行

go mod init main
并运行项目
go run main.go
但出现错误
main.go:1:1: expected 'package', found "gobot.io/x/gobot"

对于依赖关系,我这样做:

go get -v gobot.io/x/gobot
go get -v gobot.io/x/gobot/platforms/ble
go setup-project
1个回答
0
投票

首先,您必须向项目添加依赖项:

go get gobot.io/x/gobot
go get gobot.io/x/gobot/platforms/[email protected]

然后,您必须更改源文件 - 添加包用法并修复导入

package main

import (
    "fmt"
    "os"
    "time"

    "gobot.io/x/gobot"
    "gobot.io/x/gobot/platforms/ble"
)

func main() {
   // without changes
}

现在应该可以正常编译了。

© www.soinside.com 2019 - 2024. All rights reserved.