質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

Q&A

1回答

1535閲覧

Go buildが通りません(mac環境)

ryo_dev

総合スコア8

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

0グッド

1クリップ

投稿2020/03/18 06:12

Go言語初心者です。
現在Chatアプリを作成中で,以下のtreeとなってます

tree

1. 2├── auth 3│   └── auth.go 4├── htmlTemplates 5│   ├── chat.html 6│   ├── signin.html 7│   8├── img 9│   └── google-logo.svg 10├── src 11│   ├── chatroom.go 12│   ├── client.go 13│   ├── handler.go 14│   ├── main.go 15│   └── message.go 16└── stylesheets 17 ├── chat-sheet.css 18 ├── signin-sheet.css

とりあえず形はできたのでCompileしようと思ったのですが、
エラーの嵐で困ってます...(/srcでbuildしてます)
GOPATHは、.zprofileでpath通してgo get objx(,gomniauth)してるんですが
どうやったらCompileできますか?

❯ go build main.go ../auth/auth.go:13:2: cannot find package "github.com/stretchr/gomniauth" in any of: /usr/local/Cellar/go/1.14/libexec/src/github.com/stretchr/gomniauth (from $GOROOT) /Users/hoge/Documents/go/src/github.com/stretchr/gomniauth (from $GOPATH) ../auth/auth.go:10:2: cannot find package "github.com/stretchr/gomniauth/providers/github" in any of: /usr/local/Cellar/go/1.14/libexec/src/github.com/stretchr/gomniauth/providers/github (from $GOROOT) /Users/hoge/Documents/go/src/github.com/stretchr/gomniauth/providers/github (from $GOPATH) ../auth/auth.go:11:2: cannot find package "github.com/stretchr/gomniauth/providers/google" in any of: /usr/local/Cellar/go/1.14/libexec/src/github.com/stretchr/gomniauth/providers/google (from $GOROOT) /Users/hoge/Documents/go/src/github.com/stretchr/gomniauth/providers/google (from $GOPATH) ../auth/auth.go:8:2: cannot find package "github.com/stretchr/objx" in any of: /usr/local/Cellar/go/1.14/libexec/src/github.com/stretchr/objx (from $GOROOT) /Users/hoge/Documents/go/src/github.com/stretchr/objx (from $GOPATH)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

src ディレクトリ配下で go mod init <モジュール名> としましょう。こうすることで src がリポジトリのルートになります。あとは必要なモジュールを go get github.com/stretchr/gomniauth などとすればコンパイルが通ると思います。

ちなみに外部公開するのであれば go mod init github.com/my/repo などとするのが一般的です

投稿2020/03/18 06:38

d_tutuz

総合スコア730

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ryo_dev

2020/03/18 07:51

回答ありがとうございます やってみたところ(go mod init exmaple.com/chat)、今度は以下のエラーがでました。 [ > go build ] go: finding module for package github.com/gorilla/websocket go: finding module for package github.com/stretchr/objx go: downloading github.com/gorilla/websocket v1.4.1 go: downloading github.com/stretchr/objx v0.2.0 go: found github.com/gorilla/websocket in github.com/gorilla/websocket v1.4.1 go: found github.com/stretchr/objx in github.com/stretchr/objx v0.2.0 main.go:7:2: local import "../auth" in non-local package ../auth/auth.go:13:2: cannot find package ../auth/auth.go:10:2: cannot find package ../auth/auth.go:11:2: cannot find package
d_tutuz

2020/03/18 08:58 編集

authとsrcが分かれているのは、別のサービスだから?みたいな理由なんですかね。 推奨されないですが、以下のように auth 側でも go mod init auth などとして src 側の go.mod に以下を追記すれば、 require auth v0.0.0 replace auth => ../auth main.go で import auth とすることで参照はできると思います。
d_tutuz

2020/03/18 15:06 編集

あるいは、単一リポジトリであれば、 ルートで go mod init exmaple.com/chat などとすれば、main.go からは "exmaple.com/chat/auth" ようにして参照できます。(そういう意図だっと思いました、失礼しました)
ryo_dev

2020/03/19 03:32

packageは無事importできました が、以下のエラーがでました # auth ../auth/auth.go:22:27: undefined: securityKey ../auth/auth.go:23:2: undefined: gomoniauth ../auth/auth.go:25:4: undefined: googleClientId ../auth/auth.go:26:4: undefined: googleClientSecurityKey ../auth/auth.go:30:4: undefined: githubClientId ../auth/auth.go:31:4: undefined: githubClientSecurityKey ../auth/auth.go:39:32: r.URL.PATH undefined (type *url.URL has no field or method PATH, but does have Path) [auth.go] コードのせておきます ↓ package auth import ( "log" "net/http" "strings" "github.com/stretchr/objx" "github.com/stretchr/gomniauth/providers/github" "github.com/stretchr/gomniauth/providers/google" "github.com/stretchr/gomniauth" ) type AuthHandler struct { Path string } //プロバイダ・モデルを生成 func SetAuthInfo() { gomniauth.SetSecurityKey(securityKey) gomoniauth.WithProviders( google.New( googleClientId, googleClientSecurityKey, "http://localhost:8080/auth/callback/google", ), github.New( githubClientId, githubClientSecurityKey, "http://localhost:8080/auth/callback/github", ), ) } //指定されたプロバイダに対して認証を行う func (a *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { segs := strings.Split(r.URL.PATH, "/") action := segs[2] //処理内容 login or callback provider_name := segs[3] //プロバイダ名 google or github switch action { case "login": //loginページから遷移時 //プロバイダモデル生成 provider, err := gomniauth.Provider(provider_name) if err != nil { log.Fatalln("プロバイダの取得に失敗") } //プロバイダ毎の認証ページurlを取得 loginUrl, err := provider.GetBeginAuthURL(nil, nil) if err != nil { log.Fatalln("認証ページの取得に失敗") } //提供された認証用のページへリダイレクト w.Header().Set("Location", loginUrl) w.WriteHeader(http.StatusTemporaryRedirect) case "callback": //認証終了後callbackされた時 //プロバイダモデル生成 provider, err := gomniauth.Provider(provider_name) if err != nil { log.Fatalln("プロバイダの取得に失敗") } //提供されたURLから認証に必要な情報取得 creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery)) if err != nil { log.Fatalln("認証情報の取得に失敗") } //認証情報を使用して、プロバイダからUserオブジェクト取得 user, err := provider.GetUser(creds) if err != nil { log.Fatalln("ユーザー情報の取得に失敗") } //プロバイダから提供されたuserオブジェクトから情報を取得 authCookieValue := objx.New(map[string]interface{}{ "name": user.Name(), //ユーザー名 "avatar_url": user.AvatarURL(), //プロフィール画像URL "provider": provider_name, }).MustBase64() //User情報をCookieに格納 http.SetCookie(w, &http.Cookie{ Name: "auth", Value: authCookieValue, Path: "/", }) //ログイン後の画面に遷移 w.Header()["Location"] = []string{a.Path} w.WriteHeader(http.StatusTemporaryRedirect) } }
d_tutuz

2020/03/19 06:22

コンパイラエラーが示しているように、securityKey などの変数が定義されていないということです。使おうとしている変数が定義されているか確認しましょう
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問