質問内容
「Go言語でつくるインタプリタ」という参考書をもとに行っているのですが,
% go test ./lexer
という命令を実行すると
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
というように出力され,実行されません.どこを修正すればよいのでしょうか.
正しい出力は以下のようになるようです.
ok monkey/lexer 0.007s
lexer.go
Go
1package lexer 2 3import "monkey/token" 4 5 6type Lexer struct { 7 input string 8 position int //入力における現在の位置(現在の文字を指し示す) 9 readPosition int //これから読み込む位置(現在の次の文字) 10 ch byte //現在検査中の文字 11} 12 13func New(input string) *Lexer { 14 l := &Lexer{input: input} 15 l.readChar() 16 return l 17} 18 19func (l *Lexer) readChar() { 20 if l.readPosition >= len(l.input) { 21 l.ch = 0 22 } else { 23 l.ch = l.input[l.readPosition] 24 } 25 l.position = l.readPosition 26 l.readPosition += 1 27} 28 29func (l *Lexer) NextToken() token.Token { 30 var tok token.Token 31 32 switch l.ch { 33 case '=': 34 tok = newToken(token.ASSIGN, l.ch) 35 case ';': 36 tok = newToken(token.SEMICOLON, l.ch) 37 case '(': 38 tok = newToken(token.LPAREN, l.ch) 39 case ')': 40 tok = newToken(token.RPAREN, l.ch) 41 case ',': 42 tok = newToken(token.COMMA, l.ch) 43 case '+': 44 tok = newToken(token.PLUS, l.ch) 45 case '{': 46 tok = newToken(token.LBRACE, l.ch) 47 case '}': 48 tok = newToken(token.RBRACE, l.ch) 49 case 0: 50 tok.Literal = "" 51 tok.Type = token.EOF 52 } 53 l.readChar() 54 return tok 55} 56 57func newToken(tokenType token.TokenType, ch byte) token.Token { 58 return token.Token{Type: tokenType, Literal: string(ch)} 59} 60
現状1
MacOSで,ディレクトリ構成は以下のようになっています.
~/Go/monkey/lexer
monkeyディレクトリ,lexerディレクトリで上記の命令を実行しても同じ文が表示されます.
現状2
,以下に示す,hello.goを次のように命令した場合はきちんと望み通りの出力が出てきます.
Go
1package main 2 3import "fmt" 4 5func main() { 6 fmt.Printf("hello, world\n") 7}
% go run hello.go hello, world
やったこと
やったこと
・Homebrewのインストール
https://brew.sh/index_ja
・% brew -v
Homebrew 3.5.2
で入ったことの確認
・% brew install direnv
でdirenvのインストール
・~/.zshrcに
export GOPATH=$HOME/go
export EDITOR=vi
eval "$(direnv hook bash)"
を書き込む
・source .zshrc
・https://qiita.com/hinastory/items/336c2d944fdb8ae11efb
を確認したがあまり対処方法がわからなかった.
追記
.zshrcに次のものを書き加えた.
export GO111MODULE=off
こののち,実行してみると以下のようになりました.
monkey % go test ./lexer found packages main (hello.go) and lexer (lexer.go) in /Users/SSS/Go/monkey/lexer lexer/lexer.go:4:2: cannot find package "monkey/token" in any of: /usr/local/go/src/monkey/token (from $GOROOT) /Users/SSS/go/src/monkey/token (from $GOPATH)
packageが見つからないのは何故?
tokenディレクトリはmonkeyディレクトリの下,つまりlexerディレクトリと同じ階層にあります.
~/Go/monkey/lexer, token
追記2
vscode上でそもそも
import "monkey/token"
がエラーとして強調されていて
could not import monkey/token (cannot find package "monkey/token" in any of /usr/local/go/src/monkey/token (from $GOROOT) /Users/SSS/go/src/monkey/token (from $GOPATH))

回答1件
あなたの回答
tips
プレビュー