【问题标题】:Builds at commandline but fails to build as gae app在命令行构建但无法构建为 gae 应用程序
【发布时间】:2016-05-25 00:01:06
【问题描述】:

在命令行构建没有问题:

Darians-MacBook-Pro:gdriveweb darianhickman$ go build helloworld/hello.go

Darians-MacBook-Pro:gdriveweb darianhickman$

本地主机错误:8080/

无法构建 Go 应用程序。

(执行命令:/Users/darianhickman/go_appengine/goroot/bin/go-app-builder -app_base /Users/darianhickman/gowork/src/bitbucket.org/darian_hickman/gdriveweb/helloworld -arch 6 -dynamic -goroot /Users/darianhickman/go_appengine/goroot -nobuild_files ^^$ -unsafe -gopath /Users/darianhickman/gowork -binary_name _go_app -extra_imports appengine_internal/init -work_dir /var/folders/fk/wknp5jzn53gbgbml0yn695_m0000gn/T/tmpsHFP6tappengine-go-bin - gcflags -I,/Users/darianhickman/go_appengine/goroot/pkg/darwin_amd64_appengine -ldflags -L,/Users/darianhickman/go_appengine/goroot/pkg/darwin_amd64_appengine hello.go) /Users/darianhickman/gowork/src/golang.org/x/net/context/ctxhttp/ctxhttp.go:35: req.Cancel 未定义(类型 *http.Request 没有字段或方法 Cancel)

2016/05/24 19:39:17 go-app-builder:构建时间:6×6g(共 469ms),0×6l(共 0) 2016/05/24 19:39:17 go-app-builder:运行 6g 失败:退出状态 1

当我研究错误时

*http.Request 没有字段或方法 Cancel

它会导致一堆关于更新到>Go1.5 的不适用帖子。

来源:

 package hello

import (
    "encoding/json"
    "fmt"
    "golang.org/x/net/context"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/api/drive/v3"
    _ "google.golang.org/appengine/urlfetch"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
    "os"
    "os/user"
    "path/filepath"
)

const (
    assetfolder = "0B-zdryEj60U_MXVkajFweXBQWHM"
)

var (
    dir *drive.FileList
)

func init() {
    http.HandleFunc("/", handler)

    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-go-quickstart.json
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

    srv, err := drive.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    dir, err = srv.Files.List().PageSize(10).
        Fields("nextPageToken, files(id, name)").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files.", err)
    }

}

func handler(w http.ResponseWriter, r *http.Request) {

    //fmt.Fprint(w, r.RequestURI)

    fmt.Fprint(w, "Files:")
    if len(dir.Files) > 0 {
        for _, i := range dir.Files {
            fmt.Fprint(w, "%s (%s)\n", i.Name, i.Id)
        }
    } else {
        fmt.Fprint(w, "No files found.")
    }
}

// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
    cacheFile, err := tokenCacheFile()
    if err != nil {
        log.Fatalf("Unable to get path to cached credential file. %v", err)
    }
    tok, err := tokenFromFile(cacheFile)
    if err != nil {
        tok = getTokenFromWeb(config)
        saveToken(cacheFile, tok)
    }
    return config.Client(ctx, tok)
}

// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
    authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
    fmt.Printf("Go to the following link in your browser then type the "+
        "authorization code: \n%v\n", authURL)

    var code string
    if _, err := fmt.Scan(&code); err != nil {
        log.Fatalf("Unable to read authorization code %v", err)
    }

    tok, err := config.Exchange(oauth2.NoContext, code)
    if err != nil {
        log.Fatalf("Unable to retrieve token from web %v", err)
    }
    return tok
}

// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
    usr, err := user.Current()
    if err != nil {
        return "", err
    }
    tokenCacheDir := filepath.Join(usr.HomeDir, ".credentials")
    os.MkdirAll(tokenCacheDir, 0700)
    return filepath.Join(tokenCacheDir,
        url.QueryEscape("drive-go-quickstart.json")), err
}

// tokenFromFile retrieves a Token from a given file path.
// It returns the retrieved Token and any read error encountered.
func tokenFromFile(file string) (*oauth2.Token, error) {
    f, err := os.Open(file)
    if err != nil {
        return nil, err
    }
    t := &oauth2.Token{}
    err = json.NewDecoder(f).Decode(t)
    defer f.Close()
    return t, err
}

// saveToken uses a file path to create a file and store the
// token in it.
func saveToken(file string, token *oauth2.Token) {
    fmt.Printf("Saving credential file to: %s\n", file)
    f, err := os.Create(file)
    if err != nil {
        log.Fatalf("Unable to cache oauth token: %v", err)
    }
    defer f.Close()
    json.NewEncoder(f).Encode(token)
}

func main() {
    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-go-quickstart.json
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

    srv, err := drive.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    r, err := srv.Files.List().PageSize(10).
        Fields("nextPageToken, files(id, name)").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files.", err)
    }

    fmt.Println("Files:")
    if len(r.Files) > 0 {
        for _, i := range r.Files {
            fmt.Printf("%s (%s)\n", i.Name, i.Id)
        }
    } else {
        fmt.Print("No files found.")
    }

}

【问题讨论】:

    标签: google-app-engine go


    【解决方案1】:

    我通过重新下载并重新安装 Go App Engine SDK 解决了这个问题。我最好的猜测是为什么它会以某种方式包含旧版本的 go。

    【讨论】:

    • GAE 使用自己的 Go 运行时/编译器,命令行使用安装的 Go 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多