LoginSignup
4
6

More than 1 year has passed since last update.

参考書「Go言語ハンズオン」go.mod file not foundのエラー

Last updated at Posted at 2021-07-18

「Go言語ハンズオン」という参考書のSection4-1をやっています。
https://www.amazon.co.jp/dp/B08XYQ1RS3

エラー内容

goバージョン 1.16.6

zsh
$ go run hello.go
hello.go:4:3: no required module provides package fyne.io/fyne/app: go.mod file not found in current directory or any parent directory; see 'go help modules'
hello.go:5:3: no required module provides package fyne.io/fyne/widget: go.mod file not found in current directory or any parent directory; see 'go help modules'
hello.go
package main

import (
  "fyne.io/fyne/app"
  "fyne.io/fyne/widget"
)

func main() {
  a := app.New()

  w := a.NewWindow("Hello")
  w.SetContent(
    widget.NewLabel("Hello Fyne!"),
  )

  w.ShowAndRun()
}

解決方法

Go1.11バージョン以降追加された機能の”Goモジュール”でパッケージ管理が必要みたいです。
その前に、僕はmacOSパッケージインストーラでGUIでGoをインストールしたのですが、goenvを使ってGoをインストールし直しました。

GUIで入れたGoをアンインストール

参考記事 https://qiita.com/Nekonecode/items/8561bbe27830090bc70c

zsh
$ sudo rm -rf /usr/local/go
$ sudo rm -rf /etc/paths.d/go

goenvでGoを再インストール

参考記事 https://github.com/syndbg/goenv/blob/master/INSTALL.md

zsh
$ git clone https://github.com/syndbg/goenv.git ~/.goenv
.zshrc
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"
zsh
$ exec $SHELL
$ goenv install -l
$ goenv install 1.16.6
$ goenv global 1.16.6
$ go version

Goモジュールの管理下に

参考記事 https://qiita.com/Syoitu/items/f221b52231703cebe8ff

zsh
$ go env -w GO111MODULE=on
zsh
# hello.goと同じ階層で
$ go mod init hello
$ go get -u fyne.io/fyne
$ go mod tidy
$ go run hello.go
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6