Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Aplikasi yang berjalan dalam runtime standar Go 1.12+ dapat menggunakan
paket apa pun yang kompatibel dengan linux/amd64.
App Engine memerlukan semua pernyataan impor dalam kode Go Anda untuk
menentukan jalur absolut. Misalnya, gunakan:
import"github.com/example/mypackage"
Menggunakan modul Go
Sebaiknya gunakan modul Go
untuk mengelola dependensi di aplikasi Go Anda. Anda dapat terus menggunakan mode GOPATH yang lebih lama
jika belum siap untuk bermigrasi
ke modul Go.
Mulai Go versi 1.22 dan yang lebih baru:
Anda tidak dapat menggunakan go get di luar modul dalam mode GOPATH lama
(GO111MODULE=off). Untuk informasi selengkapnya, lihat Alat.
Go merekomendasikan agar Anda menggunakan file go.mod untuk mengelola dependensi. Untuk menginstal
dependensi selama deployment, sertakan file go.mod di folder yang sama dengan
file app.yaml. Untuk mengetahui informasi selengkapnya tentang versi Go, dan mengelola
dependensi untuk direktori vendor, lihat GOPATH dan Modul.
Saat Anda men-deploy aplikasi, App Engine menggunakan perintah go build untuk
mem-build aplikasi dan mencocokkan perilaku Go itu sendiri. Untuk memastikan bahwa
aplikasi Anda menggunakan mode module-aware, lakukan hal berikut di lingkungan
pengembangan:
Buat file go.mod modul Anda di dalam direktori yang sama dengan file app.yaml
Anda. App Engine menelusuri direktori saat ini dan beberapa
direktori induk berurutan hingga menemukan file go.mod.
Jika App Engine tidak menemukan file go.mod, App Engine akan mengikuti
mode GOPATH.
Jika Anda menetapkan variabel lingkungan GO111MODULE,
pastikan nilai variabel mengaktifkan
mode module-aware. Saat Anda men-deploy aplikasi, App Engine akan memeriksa
lingkungan Anda untuk menemukan GO111MODULE dan mencocokkan perilaku Go itu sendiri.
App Engine hanya menerapkan setelan variabel GO111MODULE jika Anda telah
menyertakan file go.mod untuk aplikasi Anda.
Untuk Go 1.12, jangan tempatkan direktori aplikasi Anda dalam folder $GOPATH/src. Jika
aplikasi Go 1.12 Anda berada di mana saja dalam hierarki direktori $GOPATH/src,
App Engine akan mengikuti mode GOPATH meskipun Anda telah menentukan file go.mod
untuk aplikasi Anda.
Untuk Go 1.13 dan yang lebih baru, App Engine menggunakan
mode module-aware
secara default, kecuali jika GO111MODULE mengabaikan default atau file go.mod
tidak ada dalam direktori aplikasi.
Dependensi vendoring
Vendoring menyalin paket yang digunakan aplikasi Anda ke dalam direktori aplikasi,
dan tidak akan mendownload modul dari sumbernya selama proses build. Go
menyediakan perintah go build untuk
mem-vendor paket yang dibutuhkan aplikasi Anda
ke direktori bernama vendor di direktori utama aplikasi Anda.
Di Go 1.14 dan yang lebih baru, jika direktori utama aplikasi berisi direktori bernama
vendor, perintah go build dan proses deployment App Engine
akan menggunakan paket dalam direktori vendor, dan tidak akan mendownload modul.
Di Go 1.13 dan yang lebih lama, vendoring hanya tersedia jika Anda menyiapkan
lingkungan untuk menggunakan mode GOPATH.
Menggunakan dependensi pribadi
App Engine tidak dapat mendownload dependensi pribadi Anda selama proses
build, sehingga Anda harus menyertakan dependensi dengan kode aplikasi setelah
deployment.
Gunakan perintah replace dalam file go.mod untuk mendeklarasikan
dependensi pribadi. Contoh berikut mengasumsikan bahwa aplikasi Anda berada di direktori
/myapp/:
Ubah ke direktori aplikasi Anda:
cd/myapp
Buat direktori yang berisi dependensi pribadi Anda:
mkdirprivate
Pastikan dependensi pribadi Anda berada di direktori private. Salah satu
pendekatannya adalah dengan membuat symlink:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-11 UTC."],[[["\u003cp\u003eGo apps in App Engine must use absolute paths in import statements, such as \u003ccode\u003eimport "github.com/example/mypackage"\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eUsing Go modules is recommended for managing dependencies, and a \u003ccode\u003ego.mod\u003c/code\u003e file should be included in the same folder as \u003ccode\u003eapp.yaml\u003c/code\u003e for dependency installation during deployment.\u003c/p\u003e\n"],["\u003cp\u003eFor Go 1.22 and later, \u003ccode\u003ego get\u003c/code\u003e cannot be used outside of a module in legacy \u003ccode\u003eGOPATH\u003c/code\u003e mode, and the use of a \u003ccode\u003ego.mod\u003c/code\u003e file for dependency management is strongly encouraged.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine's build process uses the \u003ccode\u003ego build\u003c/code\u003e command, matching Go's behavior, and it prioritizes \u003ccode\u003emodule-aware\u003c/code\u003e mode if a \u003ccode\u003ego.mod\u003c/code\u003e file is present, or if using Go 1.13 or later, unless \u003ccode\u003eGO111MODULE\u003c/code\u003e dictates otherwise.\u003c/p\u003e\n"],["\u003cp\u003eVendoring, or copying packages to a \u003ccode\u003evendor\u003c/code\u003e directory in your app, is supported from Go 1.14 and above. And for the use of private dependencies, you should use a \u003ccode\u003ereplace\u003c/code\u003e directive in the \u003ccode\u003ego.mod\u003c/code\u003e file, and then place those dependencies within the \u003ccode\u003eprivate\u003c/code\u003e directory of the app.\u003c/p\u003e\n"]]],[],null,["# Specify dependencies\n\nApps that run in the Go 1.12+ standard runtime can use any\nlinux/amd64-compatible package.\n\nApp Engine requires all import statements in your Go code to\nspecify absolute paths. For example, use: \n\n import \"github.com/example/mypackage\"\n\nUse Go modules\n--------------\n\nWe recommend that you use [Go modules](https://blog.golang.org/using-go-modules)\nto manage dependencies in your Go app. You can continue to use the older `GOPATH` mode\nif you aren't ready to [migrate\nto Go modules](https://blog.golang.org/migrating-to-go-modules).\n\nStarting in Go **version 1.22 and later**:\n\n- You can't use `go get` outside of a module in the legacy `GOPATH` mode\n (`GO111MODULE`=`off`). For more information, see [Tools](https://tip.golang.org/doc/go1.22#:%7E:text=enables%20this%20feature.-,Tools,-Go%20command).\n\n- Go recommends that you use a `go.mod` file for managing dependencies. To install\n dependencies during deployment, include a `go.mod` file in the same folder as\n the `app.yaml` file. For more information about Go versions, and managing\n dependencies for vendor directories, see [`GOPATH` and Modules](https://pkg.go.dev/cmd/go#hdr-GOPATH_and_Modules).\n\nWhen you deploy your app, App Engine uses the `go build` command to\nbuild your app and matches the behavior of Go itself. To ensure that\nyour app uses `module-aware` mode, do the following in your development\nenvironment:\n\n- Create your module's `go.mod` file in the same directory as your `app.yaml`\n file. App Engine searches the current directory and the successive\n parent directories until it finds a [`go.mod` file](https://golang.org/cmd/go/#hdr-Defining_a_module).\n\n If App Engine doesn't find a `go.mod` file, it follows\n `GOPATH` mode.\n- If you set the [`GO111MODULE` environment variable](https://golang.org/ref/mod#mod-commands),\n make sure that the variable's value enables\n `module-aware` mode. When you deploy your app, App Engine checks your\n environment for `GO111MODULE` and matches the behavior of Go itself.\n App Engine only applies the `GO111MODULE` variable setting if you have\n included a `go.mod` file for your app.\n\n- For Go 1.12, don't locate your app directory within the `$GOPATH/src` folder. If\n your Go 1.12 app is anywhere in the `$GOPATH/src` directory tree,\n App Engine follows `GOPATH` mode even if you've defined a `go.mod` file\n for your app.\n\n For Go 1.13 and later, App Engine uses\n [`module-aware` mode](https://golang.org/cmd/go/#hdr-Module_support)\n by default, unless `GO111MODULE` overrides the default or a `go.mod` file\n doesn't exist in the app's directory.\n\n### Vendoring dependencies\n\n**Vendoring** copies the packages your app uses into the application directory\ninstead of downloading modules from their sources during the build process. Go\nprovides the `go build` command to\n[vendor the packages your app needs](https://golang.org/cmd/go/#hdr-Modules_and_vendoring)\ninto a directory named `vendor` in your app's root directory.\n\nIn Go 1.14 and later, if your app's root directory contains a directory named\n`vendor`, the `go build` command and the App Engine\ndeployment process use the packages in the vendor directory instead of\ndownloading modules.\n\nIn Go 1.13 and earlier, vendoring is only available if you set up your\nenvironment to use [`GOPATH` mode](https://golang.org/cmd/go/#hdr-GOPATH_and_Modules).\n\n### Use private dependencies\n\nApp Engine can't download your private dependencies during the build\nprocess, so you must include the dependencies with your application code upon\ndeployment.\n\nUse the `replace` directive in your `go.mod` file to declare\nprivate dependencies. The following example assumes your app is in the `/myapp/`\ndirectory:\n\n1. Change to your app directory:\n\n cd /myapp\n\n2. Create a directory containing your private dependencies:\n\n mkdir private\n\n Make sure your private dependency is in the `private` directory. One\n approach is by creating a symlink: \n\n mkdir private/private.example.com\n ln -s /path/to/private.example.com/foo private/private.example.com/foo\n\n3. Update your `go.mod` file to use the `replace` directive to use the `private`\n directory for your dependency:\n\n go mod edit -replace=private.example.com/foo=./private/private.example.com/foo\n\n Your `go.mod` file should now look like: \n\n ### Final `go.mod` file\n\n module private.example.com/myapp\n\n require private.example.com/foo v1.2.3\n\n replace private.example.com/foo =\u003e ./private/private.example.com/foo\n\n ### Original `go.mod` file\n\n module private.example.com/myapp\n\n require private.example.com/foo v1.2.3\n\n4. Don't modify how you import and use your private package. Your `import`\n statement should look like this:\n\n import \"private.example.com/foo\"\n\n5. Include your private dependency in your deployment by deploying your app:\n\n gcloud app deploy"]]