9.7 KiB
۱-۱ نصب
سه روش برای نصب Go
راه های زیادی برای تنظیم محیز توسعه ی Go بر روی کامپیوتر وجود دارند، و شما می توانید از هر یک از این روش ها به دلخواه خود استفاده کنید. به طور کلی سه روش زیر، به عنوان رایج ترین روش ها شناخته می شوند:
- نصب پکیج های رسمی.
- تیم توسعه ی Go پکیج های جامعی را برای ویندوز، لینوکس، مک و سایر سیستم عامل ها فراهم کرده است. این روش نصب، شاید ساده ترین روش ممکن برای شروع باشد. شما می توانید به صفحه ی دانلود Golang مراجعه کرده و فایل نصبی مورد نظر خود را دانلود کنید.
- نصب Go از طریق کد منبع.
- این روش بین توسعه دهندگان سیستم های شبه یونیکس مرسوم و شناخته شده است.
- استفاده از ابزارهای شخص سوم (third-party)
- ابزارها و پکیج های زیادی برای نصب Go وجود دارند، که شما می توانید از آن ها استفاده کنید. برای مثال apt-get در اوبونتو و homebrew در مک.
اگر به هر دلیلی احتیاح دارید بیشتر از یک نسخه از Go را بر روی یک کامپیوتر داشته باشید، کافیست نگاهی به ابزار GVM داشته باشید، که یکی از بهترین ابزارهای موجود برای این منظور می باشد. در غیر این صورت همیشه می توانید این کار را به صورت دستی هم انجام دهید.
نصب از طریق کد منبع
برای کامپایل Go 1.5 به بالا، کافیست فقط یکی از ورژن های قبلی Go را بر روی سیستم خود داشته باشید، چرا که Go از ویژگی بوت استرپ پشتیبانی می کند. یعنی برای کامپایل Go، به خود Go احتیاج دارید.
برای کامپایل Go 1.4 به پایین، شما به یک کامپایلر C بر روی سیستم خود احتیاج دارید. چراکه هنوز بخش هایی از Go به زبان C و همچنین اسمبلر AT&T نوشته شده است.
اگر از سیستم عامل مک استفاده می کنید و Xcode بر روی سیستم شما نصب است، شما کامپایلر مورد نیاز خود را دارید.
بر روی سیستم های شبه یونیکس، باید gcc یا کامپایلر مشابه نصب باشد. برای مثال، با استفاده از مدیر بسته apt-get (که در اوبونتو استفاده می شود)، کامپایلرهای مورد نیاز خود را به صورت زیر می توانید نصب کنید.
در این مرحله، دستورات زیر را اجرا کنید تا از کد منبع Go یک کلون گرفته و آن را کامپایل نمایید.( با این دستور، سورس کد مورد نیاز در دایرکتوری جاری شما کلون می شود. قبل از اینکه ادامه دهید، مسیر کاری خود را تغییر دهید. این امر ممکن است کمی زمان ببرد. )
بر روی ویندوز، همین کار را می توان با اجرای all.bat انجام داد.
اگر از ویندوز استفاده می کنید، پکیج های نصب به صورت خودکار تنظیمات لازم در محیط متغیرهای شما را اعمال می کنند. در سیستم های شبه یونیکس، احتیاج است که این متغیرها به صورت دستی تنظیم شوند که در ادامه نحوه تنظیم آن ها را مشاهده خواهید کرد. ( اگر ورژن Go شما بیشتر از ۱.۰ باشد، احتیاجی به تنظیم متغیر $GOBIN نمی باشد، و در واقع این متغیر به صورت خودکار به مسیر $GOROOT/bin مرتبط می شود. در فصل بعد بیشتر به این موضوع می پردازیم.)
Figure 1.1 Information after installing from source code
Once you see the usage information of Go, it means you have successfully installed Go on your computer. If it says "no such command", check that your $PATH environment variable contains the installation path of Go.
Using the standard installation packages
Go has one-click installation packages for every supported operating system. These packages will install Go in /usr/local/go (c:\Go in Windows) by default. Of course this can be modified, but you also need to change all the environment variables manually as I've shown above.
How to check if your operating system is 32-bit or 64-bit?
Our next step depends on your operating system type, so we have to check it before we download the standard installation packages.
If you are using Windows, press Win+R and then run the command tool. Type the systeminfo command and it will show you some useful system information. Find the line that says "system type" -if you see "x64-based PC" that means your operating system is 64-bit, 32-bit otherwise.
I strongly recommend downloading the 64-bit package if you are a Mac user, as Go no longer supports pure 32-bit processors on Mac OSX.
Linux users can type uname -a in the terminal to see system information.
A 64-bit operating system will show the following:
<some description> x86_64 x86_64 x86_64 GNU/Linux
// some machines such as Ubuntu 10.04 will show as following
x86_64 GNU/Linux
32-bit operating systems instead show:
<some description> i686 i686 i386 GNU/Linux
Mac
Go to the download page, choose go1.4.2.darwin-386.pkg (The later version has no 32-bit download.)for 32-bit systems and go1.8.3.darwin-amd64.pkg for 64-bit systems. Going all the way to the end by clicking "next", ~/go/bin will be added to your system's $PATH after you finish the installation. Now open the terminal and type go. You should see the same output shown in figure 1.1.
Linux
Go to the download page, choose go1.8.3.linux-386.tar.gz for 32-bit systems and go1.8.3.linux-amd64.tar.gz for 64-bit systems. Suppose you want to install Go in the $GO_INSTALL_DIR path. Uncompress the tar.gz to your chosen path using the command tar zxvf go1.8.3.linux-amd64.tar.gz -C $GO_INSTALL_DIR. Then set your $PATH with the following: export PATH=$PATH:$GO_INSTALL_DIR/go/bin. Now just open the terminal and type go. You should now see the same output displayed in figure 1.1.
Windows
Go to the download page, choose go1.8.3.windows-386.msi for 32-bit systems and go1.8.3.windows-amd64.msi for 64-bit systems. Going all the way to the end by clicking "next", c:/go/bin will be added to path. Now just open a command line window and type go. You should now see the same output displayed in figure 1.1.
Use third-party tools
GVM
GVM is a Go multi-version control tool developed by a third-party, like rvm for ruby. It's quite easy to use. Install gvm by typing the following commands in your terminal:
bash < <(curl -s -S -L https://raw.github.com/moovweb/gvm/master/binscripts/gvm-installer)
Then we install Go using the following commands:
gvm install go1.8.3
gvm use go1.8.3
After the process has finished, you're all set.
apt-get
Ubuntu is the most popular desktop release version of Linux. It uses apt-get to manage packages. We can install Go using the following commands.
sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang-go
wget
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
sudo tar -xzf go1.8.3.linux-amd64.tar.gz -C /usr/local
# Go environment
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
export GOPATH=$HOME/gopath
Starting from go 1.8, The GOPATH environment variable now has a default value if it is unset. It defaults to $HOME/go on Unix and %USERPROFILE%/go on Windows.
Homebrew
Homebrew is a software management tool commonly used in Mac to manage packages. Just type the following commands to install Go.
- Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Install Go
brew update && brew upgrade
brew install go
Links
- Directory
- Previous section: Go environment configuration
- Next section: $GOPATH and workspace
