1. 程式人生 > >The App Engine SDK and workspaces (GOPATH)

The App Engine SDK and workspaces (GOPATH)

9 January 2013

Introduction

When we released Go 1 we introduced the go tool and, with it, the concept of workspaces. Workspaces (specified by the GOPATH environment variable) are a convention for organizing code that simplifies fetching, building, and installing Go packages. If you're not familiar with workspaces, please read

this article or watch this screencast before reading on.

Until recently, the tools in the App Engine SDK were not aware of workspaces. Without workspaces the "go get" command cannot function, and so app authors had to install and update their app dependencies manually. It was a pain.

This has all changed with version 1.7.4 of the App Engine SDK. The dev_appserver and appcfg tools are now workspace-aware. When running locally or uploading an app, these tools now search for dependencies in the workspaces specified by the GOPATH environment variable. This means you can now use "go get" while building App Engine apps, and switch between normal Go programs and App Engine apps without changing your environment or habits.

For example, let's say you want to build an app that uses OAuth 2.0 to authenticate with a remote service. A popular OAuth 2.0 library for Go is the oauth2 package, which you can install to your workspace with this command:

go get golang.org/x/oauth2

When writing your App Engine app, import the oauth package just as you would in a regular Go program:

import "golang.org/x/oauth2"

Now, whether running your app with the dev_appserver or deploying it with appcfg, the tools will find the oauth package in your workspace. It just works.

Hybrid stand-alone/App Engine apps

The Go App Engine SDK builds on Go's standard net/http package to serve web requests and, as a result, many Go web servers can be run on App Engine with only a few changes. For example, godoc is included in the Go distribution as a stand-alone program, but it can also run as an App Engine app (godoc serves golang.org from App Engine).

But wouldn't it be nice if you could write a program that is both a stand-alone web server and an App Engine app? By using build constraints, you can.

Build constraints are line comments that determine whether a file should be included in a package. They are most often used in code that handles a variety of operating systems or processor architectures. For instance, the path/filepath package includes the file symlink.go, which specifies a build constraint to ensure that it is not built on Windows systems (which do not have symbolic links):

// +build !windows

The App Engine SDK introduces a new build constraint term: "appengine". Files that specify

// +build appengine

will be built by the App Engine SDK and ignored by the go tool. Conversely, files that specify

// +build !appengine

are ignored by the App Engine SDK, while the go tool will happily build them.

The goprotobuf library uses this mechanism to provide two implementations of a key part of its encode/decode machinery: pointer_unsafe.go is the faster version that cannot be used on App Engine because it uses the unsafe package, while pointer_reflect.go is a slower version that avoids unsafe by using the reflect package instead.

Let's take a simple Go web server and turn it into a hybrid app. This is main.go:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe("localhost:8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello!")
}

Build this with the go tool and you'll get a stand-alone web server executable.

The App Engine infrastructure provides its own main function that runs its equivalent to ListenAndServe. To convert main.go to an App Engine app, drop the call to ListenAndServe and register the handler in an init function (which runs before main). This is app.go:

package main

import (
    "fmt"
    "net/http"
)

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

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello!")
}

To make this a hybrid app, we need to split it into an App Engine-specific part, an stand-alone binary-specific part, and the parts common to both versions. In this case, there is no App Engine-specific part, so we split it into just two files:

app.go specifies and registers the handler function. It is identical to the code listing above, and requires no build constraints as it should be included in all versions of the program.

main.go runs the web server. It includes the "!appengine" build constraint, as it must only included when building the stand-alone binary.

// +build !appengine

package main

import "net/http"

func main() {
    http.ListenAndServe("localhost:8080", nil)        
}

To see a more complex hybrid app, take a look at the present tool.

Conclusions

We hope these changes will make it easier to work on apps with external dependencies, and to maintain code bases that contain both stand-alone programs and App Engine apps.

相關推薦

The App Engine SDK and workspaces (GOPATH)

9 January 2013 Introduction When we released Go 1 we introduced the go tool and, with it, the concept of workspa

Go App Engine SDK 1.5.5 released

11 October 2011 Today we released version 1.5.5 the Go App Engine SDK. You can download it from the App Engi

building a blog using google app engine and python

Today, I created two videos as a continuation of my YouTube series (). This post will be short because you might need just a little over 5 minutes a pie

Go and Google App Engine

10 May 2011 Google’s App Engine provides a reliable, scalable, easy way to build and deploy applications for

I created the exact same app in React and Vue. Here are the differences.

So you can see that we have passed the same data into both, but they’re simply labelled differently. So passing initial data into our components is very,

Ask HN: What are the spreadsheet use cases and pain points in your SAAS app

Excel / csv uploads are commonly used to get data into a saas app, is this a burden to implement for you?Do you use spreadsheets as part of workflows as we

GoLand 2018.3 Beta: Change Signature refactoring, debugging Google App Engine, Testify, Go core dumps, and more

The upcoming GoLand 2018.3 release has reached the Beta milestone! If you’re new to our release cycles, we announce a Beta when we’ve packed e

SDK and App Events

SDK Setup and Adding App Events After you complete app set up, you can only optimize delivery of your ads for LINK_CLICK or clicks on the actual ad. You n

Two Go Talks: "Lexical Scanning in Go" and "Cuddle: an App Engine Demo"

1 September 2011 On Tuesday night Rob Pike and Andrew Gerrand each presented at the Sydney Google Technology

The app.Configuration 'compile' is obsolete and has been replaced with 'implementation'.

For an Android project, the new configurations came with the Android Gradle Plugin 3.0. So unless you are still using the 2.x version of A

What is the difference between Kill and Kill -9 command in Unix?

data esp osi lin mil print ren win sku w difference kill -9 pid and kill pid command - Ask Ubuntu https://askubuntu.com/questions/7918

My app status is Ready for Sale but I cannot see my app on the App Store. Why? 為什麽審核通過後 appstore中搜不到我的app

one soci orm event 什麽 live pstore follow following 這是蘋果的官方解答 The following factors could prevent your app from showing up on the App St

HDU 4489 The King’s Ups and Downs

stdin algo pll class tar mem blank mes nbsp http://acm.hdu.edu.cn/showproblem.php?pid=4489 題意:有n個身高不同的人,計算高低或低高交錯排列的方法數。 思路:可以按照身高順序

[Git]Please make sure you have the correct access rights and the repository exists

perm not 配置 兩個 需要 acc repos http 機器 這個問題是這樣,需要在已有github賬號的A機器上,再創建一個github賬號,新賬號創建完畢,將代碼通過機器A push上之後,再另一臺機器B,clone 這個項目時報出了如下錯誤: Permiss

Count the consecutive zero bits (trailing) on the right with multiply and lookup

驗證 %d vol tip inf mat cit number failed 我在網上看到了一點神奇的代碼,用來計算一個數字末尾連續零的個數。 剛好我在優化一個I2C讀寫函數(只寫入I2C特定bit),覺得這個很有用。經過嘗試,確實沒問題。 下面我隆重介紹一下: Coun

Git clone時出現Please make sure you have the correct access rights and the repository exists.問題已解決。

already 下載 cor 兩個 .com pair 賬戶 same 使用 看了好多資料終於搞定了git 中clone命令報錯這個問題,廢話不多說直接上步驟希望對大家有幫助。 1 刪除.ssh文件夾(直接搜索該文件夾)下的known_hosts(手動刪除即可

並發與並行的區別 The differences between Concurrency and Parallel

並發與並行 mark 並行執行 程序 log tween 計算 線程 currency 邏輯控制流 在程序加載到內存並執行的時候(進程),操作系統會通過讓它和其他進程分時段占用CPU(CPU slices)讓它產生自己獨占CPU的假象(同時通過虛擬內存讓它產生獨占內存的假

The Usage of Lambda and Heap in the C++ STL

ner class eap cto con c++ stl nts been nta The Usage of Lambda and Heap in the C++ STL Heap In c++ STL, the heap had been implemented as

警告 All interface orientations must be supported unless the app requires full screen.

HR mar pos ref -s 圖片 targe BE ios-sdk   編譯時警告 All interface orientations must be supported unless the app requires full screen.   App 默認是

Google App Engine、Google Kubernetes Engine、Google Compute Engine的區別

Kubernete style TP log 網址 images put ges 分享 一、官網解釋 原文網址: https://cloud.google.com/docs/choosing-a-compute-option Google App Engine、