Files
build-web-application-with-…/fa/working/01.2.md
2018-07-03 15:45:52 +04:30

8.2 KiB
Raw Blame History

1.2 $GOPATH و فضای کاری

$GOPATH

Go با معرفی دایرکتوری $GOPATH روش منحصر به فردی برای مدیریت فایل های حاوی کد دارد. در این مسیر تمام کدهای مرتبط با go در سیستم شما قرار گرفته است. توجه داشته باشید که این دایرکتوری با متغیر محیطی $GOROOT، که مشخص می‌کند go در چه مسیری نصب شده است، تفاوت دارد. قبل از اینکه از این زبان برای برنامه‌نویسی استفاده کنیم باید متغیر $GOPATH را تعریف کنیم. در سیستم‌های شبه یونیکس (*nix) فایلی به نام .profile وجود دارد که باید دستور زیر را به انتهای این فایل اضافه کنیم. مفهوم استفاده از gopath یک مفهوم جدید محسوب می شود که طی آن، هر کد go را می توان در هر زمان و بدون هیچ گونه ابهامی لینک نمود.

در ورژن go 1.8 به بعد، اگر متغیر محیطی GOPATH تنظیم نشود، یک مقدار پیش فرض برای آن در نظر گرفته می شود که در یونیکس مقدار $HOME/go و در ویندوز مقدار %USERPROFILE%/go را دارد.

در سیستم‌های شبه یونیکسی، این متغیر را می‌توان به صورت زیر مورد استفاده قرار داد: export GOPATH=${HOME}/mygo

در ویندوز، ابتدا باید یک متغیر محیطی به نام GOPATH ایجاد کنید، که با مقدار c:\mygo مقداردهی شده باشد. ( این مقدار بستگی به این دارد که فضای کاری شما در کجا قرار گرفته باشد )

طبیعتا مشکلی ندارد که در فضای کاری شما، بیشتر از یک مقدار برای $GOPATH در نظر گرفته شود، فقط توجه داشته باشید که با استفاده از : این مقادیر را از هم مجزا کنید (در ویندوز باید از ; استفاده کنید). در این حالت، دستور go get محتوای اولین مسیر شما را در $GOPATH قرار می دهد. معمولا توصیه می‌شود که از نصب ورژن‌های متعدد Go خودداری شود. در برترین حالت ممکن است فولدری با نام پروژه خود در مسیر $GOPATH ایجاد کنید، که باعث می‌شود همه چیز به هم بریزد. چون وقتی در مسیر $GOPATH پوشه ای ساخته می شود، آن پوشه را به عنوان یک پکیج/بسته می شناسد. بنابراین وقتی از دستور go get استفاده می‌کنید، دنبال بسته ای هم نام با فولدر شما می‌گردد و طبیعتا آن را پیدا نمی‌کند. پس لطفا از قراردادهای تعریف شده برای برنامه‌نویسی پیروی کنید چون حتما برای تعریف هر یک از آن ها دلیلی وجود داشته است.

در $GOPATH، شما باید سه فولدر به صورت زیر داشته باشید:

  • src برای سورس فایل‌هایی که پسوند .go، .c، .g، .s دارند.
  • pkg برای فایل‌های کامپایل شده ای که پسوند .a دارند.
  • bin برای فایل‌های اجرایی.

در این کتاب، ما از mygo به عنوان تنها مقداری که در متغیر $GOPATH قرار گرفته استفاده می کنیم.

دایرکتوری بسته‌ها

Create package source files and folders like $GOPATH/src/mymath/sqrt.go (mymath is the package name) ( Author uses mymath as his package name, and the same name for the folder that contains the package source files)

Every time you create a package, you should create a new folder in the src directory, with the notable exception of main, for which main folder creation is optional. Folder names are usually the same as the package that you are going to use. You can have multi-level directories if you want to. For example, if you create the directory $GOPATH/src/github.com/astaxie/beedb, then the package path would be github.com/astaxie/beedb. The package name will be the last directory in your path, which is beedb in this case.

Execute following commands. ( Now author goes back to talk examples )

cd $GOPATH/src
mkdir mymath

Create a new file called sqrt.go, type the following content to your file.

// Source code of $GOPATH/src/mymath/sqrt.go
package mymath

func Sqrt(x float64) float64 {
	z := 0.0
	for i := 0; i < 1000; i++ {
		z -= (z*z - x) / (2 * x)
	}
	return z
}

Now my package directory has been created and it's code has been written. I recommend that you use the same name for your packages as their corresponding directories, and that the directories contain all of the package source files.

کامپایل بسته‌ها

We've already created our package above, but how do we compile it for practical purposes? There are two ways to do this.

  1. Switch your work path to the directory of your package, then execute the go install command.
  2. Execute the above command except with a file name, like go install mymath.

After compiling, we can open the following folder.

cd $GOPATH/pkg/${GOOS}_${GOARCH}
// you can see the file was generated
mymath.a

The file whose suffix is .a is the binary file of our package. How do we use it?

Obviously, we need to create a new application to use it.

Create a new application package called mathapp.

cd $GOPATH/src
mkdir mathapp
cd mathapp
vim main.go

Write the following content to main.go.


//$GOPATH/src/mathapp/main.go source code.
package main

import (
	"mymath"
	"fmt"
)

func main() {
	fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
}

To compile this application, you need to switch to the application directory, which in this case is $GOPATH/src/mathapp, then execute the go install command. Now you should see an executable file called mathapp was generated in the directory $GOPATH/bin/. To run this program, use the ./mathapp command. You should see the following content in your terminal.

Hello world. Sqrt(2) = 1.414213562373095

نصب بسته‌های ریموت

Go has a tool for installing remote packages, which is a command called go get. It supports most open source communities, including Github, Google Code, BitBucket, and Launchpad.

go get github.com/astaxie/beedb

You can use go get -u … to update your remote packages and it will automatically install all the dependent packages as well.

This tool will use different version control tools for different open source platforms. For example, git for Github and hg for Google Code. Therefore, you have to install these version control tools before you use go get.

After executing the above commands, the directory structure should look like following.

$GOPATH
	src
	 |-github.com
	 	 |-astaxie
	 	 	 |-beedb
	pkg
	 |--${GOOS}_${GOARCH}
	 	 |-github.com
	 	 	 |-astaxie
	 	 	 	 |-beedb.a

Actually, go get clones source code to the $GOPATH/src of the local file system, then executes go install.

You can use remote packages in the same way that we use local packages.

import "github.com/astaxie/beedb"

ساختار کامل دایرکتوری

If you've followed all of the above steps, your directory structure should now look like the following.

bin/
	mathapp
pkg/
	${GOOS}_${GOARCH}, such as darwin_amd64, linux_amd64
  mymath.a
  github.com/
    astaxie/
      beedb.a
src/
	mathapp
		main.go
	mymath/
		sqrt.go
	github.com/
		astaxie/
			beedb/
				beedb.go
				util.go

Now you are able to see the directory structure clearly; bin contains executable files, pkg contains compiled files and src contains package source files.

(The format of environment variables in Windows is %GOPATH%, however this book mainly follows the Unix-style, so Windows users need to replace these yourself.)

لینک‌ها