Golang(1)Installation and Web Application with Golang

前端之家收集整理的这篇文章主要介绍了Golang(1)Installation and Web Application with Golang前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Golang(1)Installation and Web Application with Golang

1. Installation
Download the file from herehttps://go.googlecode.com/files/go1.2.darwin-amd64-osx10.8.tar.gz

Edit the path configuration
>sudo vi ~/.profile
export GOROOT=/opt/go
export PATH=/opt/go/bin:$PATH
>. ~/.profile

Verify the installation
>go version
go version go1.2 darwin/amd64

>vi hello.go
package main import "fmt" func main() { fmt.Printf("hello,first installation\n")
}

>go run hello.go
hello,first installation

And few days later,I update the version to 1.2.1.

How to check my System
>uname -m
x86_64

2. Reading Book - Environment
The project is in easygo.

First of all,I create a main class that will be the executable entry for my application.
src/com/sillycat/easygoapp/main.go
package main

import (
"fmt"
)

func main() {
fmt.Printf("Hello,sillycat. \n")
}

The I use the command to build the source codes
>go build com/sillycat/easygoapp

This step will generate the binary file in the root directory named easygoapp.
>go install com/sillycat/easygoapp

This will install the binary file into directory
bin/easygoapp

Run the command
>bin/easygoapp
Hello,sillycat.

And we can also create a function class here
/src/com/sillycat/easygoapp/math/sqrt.go
package math

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

Install the function
>go install com/sillycat/easygoapp/math

It will generate the .a file
/Users/carl/work/easy/easygo/pkg/darwin_amd64/com/sillycat/easygoapp/math.a

And the main file will be changed as follow:
package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("Hello,sillycat. \n")
fmt.Printf("Math result = %v\n",math.Sqrt(10))
}

3. Go Command
go build
Compile and Test,but for example,the normal package math,if you run the command go build,no file will generate.
Only >go install will generate the .a file.

If it is a main package,it will generate a executable file. Or we can say
>go build -o file path/filename

Default will compile all the .go files under the current directory.

go clean

go fmt

go get

go test
run all the files named like this *_test.go

go list
list all the package under current directory.

go run
>go run src/com/sillycat/easygoapp/main.go

4. Prepare the IDE
4.1 LiteIDE
https://github.com/visualfc/liteide
I have the go environment,and I download the binary file for MAC

Install gocode
>go get -u github.com/nsf/gocode

Find LiteEnv in the References and set the file darwin64.env
GOROOT=/opt/go
Try to start debug,but I get this Error Message
22:56:53 GdbDebugger: /usr/local/bin/gdb was not found on system PATH (hint: is GDB installed?)
22:56:53 LiteDebug: Failed to start debugger
Solution:
Install GDB
Check my env
>gdb --version
-bash: gdb: command not found

Find the software tool there
http://www.gnu.org/software/gdb/download/
http://ftp.gnu.org/gnu/gdb/

>wget http://ftp.gnu.org/gnu/gdb/gdb-7.7.tar.gz
>tar zxvf gdb-7.7.tar.gz

>./configure --prefix=/Users/carl/tool/gdb-7.7
>make
>sudo make install

>bin/gdb --version
GNU gdb (GDB) 7.7

Soft link the gdb
>sudo ln -s /Users/carl/tool/gdb-7.7 /opt/gdb-7.7
>sudo ln -s /opt/gdb-7.7 /opt/gdb

Find .profile and add
export PATH=/opt/gdb/bin:$PATH

Still do not know how to debug in LiteIDE

4.2 Sublime
sublime + gosublime + gocode + margo

I already have sublime 3. There should be some difference,but most of them should be the same.

Install Package Control
View > Show Console,or use command ‘ctrl’ + `.

Here is the command for sublime text 3.
import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s),please try manual install' % (dh,h)) if dh != h else open(os.path.join( ipp,pf),'wb' ).write(by)

After that,I restart my sublime,and I saw the menu ‘References’ ——> ‘Package Control’,Great,then we can install the plugins we need for sublime 3.

‘Command’ + ’Shift’ + P,or ’Tools’ —> ‘Command Palette’

Type ‘Install Package’ and confirm.

Install ’GoSublime’
Install ’SidebarEnhancements’

Install gocode
>go get -ugithub.com/nsf/gocode

So it looks great right now.

4.3 Vim
…snip…

4.4 Emacs
…snip...

4.5 Eclipse
…snip…

4.6 IntelliJ IDEA
Find Plugins and install ‘Golang’

Error Message:
Problem with env variables
GOPATH environment variable is empty or could not be detected properly.
This means that some tools like go run or go fmt might not run properly.
See instructions on how to fix this. (show balloon)

Solution:
https://github.com/go-lang-plugin-org/go-lang-idea-plugin/blob/master/Missing%20ENV.md

>sudo vi /etc/launchd.conf
#limit maxfiles 16384 32768 setenv GOROOT /opt/go setenv GOPATH /Users/carl/work/easy/easygo:/Users/carl/work/go

Hope it works.

References:
http://golang.org/doc/install

Books
http://tour.golang.org/#1
https://github.com/astaxie/build-web-application-with-golang

https://github.com/go-lang-plugin-org/go-lang-idea-plugin/blob/master/Missing%20ENV.md

Install GDB
http://www.goinggo.net/2013/06/installing-go-gocode-gdb-and-liteide.html

sublime
http://lucifr.com/2011/08/31/sublime-text-2-tricks-and-tips/
https://sublime.wbond.net/installation#ST3

原文链接:https://www.f2er.com/go/191038.html

猜你在找的Go相关文章