为什么从PHP 转到Golang?
yamlyaml

PHP 和Golang 的效能我想毋庸置疑是后者比较快(而且是以倍数来算),也许有的人会认为两种不应该被放在一起比较,但Golang 本身就是偏向Web 开发的,所以这也是为什么我考虑转用Golang 的原因,起初我的考虑有几个:Node.js 和Rust 还有最终被选定的Golang;先谈谈Node.js 吧。

Node.js

Node.js的效能可以说是快上PHP 3.5倍至6倍左右,而且撰写的语言还是JavaScript,蒸蚌,如此一来就不需要学习新语言了!搭配Babel更可以说是万能,不过那跟「跳跳虎」一样的Async逻辑还有那恐怖的Callback Hell,有人认为前者是种优点,这点我不否认,但是对学习PHP的我来说太过于"Mind Fuck",至于后者的Callback Hell虽然有Promise,但是那又是另一个「Then Hell」的故事了。相较于Golang之下,Node.js似乎就没有那么吸引我了。你确实可以用Node.js写出很多东西,不过那V8引擎的效能仍然有限,而且要学习新的事物,不就应该是「全新」的吗;)?

Rust

在抛弃改用Node.js 之后我曾经花了一天的时间尝试Rust 和Iron 框架,嗯⋯⋯Rust 太强大了,强大到让我觉得Rust 不应该用在这里,这想法也许很蠢,但Rust 让我觉得适合更应该拿来用在系统或者是部分底层的地方,而不应该是网路服务。

Golang

go fmtgo docgo testgo get;
还请先阅读⋯

当我在撰写这份文件的时候我会先假设你有一定的基础,你可以先阅读下列的手册,他们都很不错。


定义变数-Variables

你能够在PHP 里面想建立一个变数的时候就直接建立,夭寿赞,是吗?

PHP

$a = "foo";
$b = "bar";

Golang

蒸蚌!那么Golang 呢?在Golang 中变数分为几类:「新定义」、「预先定义」、「自动新定义」、「覆盖」。让我们来看看范例:

// 新定義:定義新的 a 變數為字串型別,而且值是「foo」
var a string = "foo"

// 預先定義:先定義一個新的 b 變數為字串型別但是不賦予值
var b string

// 自動新定義:讓 Golang 依照值的內容自己定義新變數的資料型態
c := "bar"

// 覆蓋:先前已經定義過 a 了,所以可以像這樣直接覆蓋其值
a = "fooooooo"  

输出-Echo
echo

PHP

echo "Foo"; // 輸出:Foo

$A = "Bar"
echo $A; // 輸出:Bar

$B = "Hello"
echo $B . ", world!"; // 輸出:Hello, world!

$C = [1, 2, 3];
echo var_dump($C); // 輸出:array(3) {[0]=>int(1) [1]=>int(2) [2]=>int(3)}  

Golang

fmt
fmt.Println("Foo") // 輸出:Foo

A := "Bar"  
fmt.Println(A) // 輸出:Bar

B := "Hello"  
fmt.Printf("%s, world!", B) // 輸出:Hello, world!

C := []int{1, 2, 3}  
fmt.Println(C) // 輸出:[1 2 3]  

函式-Function

这很简单,而且两个语言的用法相差甚少,下面这是PHP:

PHP

function test() {  
    return "Hello, world!";
}

echo test(); // 輸出:Hello, world!  

Golang

只是Golang 稍微聒噪了一点,你必须在函式后面宣告他最后会回传什么资料型别。

func test() string {  
    return "Hello, world!"
}

fmt.Println(test()) // 輸出:Hello, world!  

多值回传-Multiple Value

在PHP 中你要回传多个资料你就会用上阵列,然后将资料放入阵列里面,像这样。

PHP

function test() {  
    return ['username' => 'YamiOdymel', 
            'time'     => 123456];
}
$data = test();

echo $data['username'], $data['time']; // 輸出:YamiOdymel 123456  

Golang

然而在Golang 中你可以不必用到一个阵列,函式可以一次回传多个值:

func test() (string, int) {  
    return "YamiOdymel", 123456
}
username, time := test()

fmt.Println(username, time) // 輸出:YamiOdymel 123456  

匿名函式-Anonymous Function

两个语言的撰写方式不尽相同。

PHP

$a = function() {
    echo "Hello, world!";
};

$a(); // 輸出:Hello, world!

Golang

a := func() {  
    fmt.Println("Hello, world!")
}

a() // 輸出:Hello, world!  

多资料储存型态-Stores

主要是PHP 的阵列能做太多事情了,所以在PHP 里面要储存什么用阵列就好了。

PHP

$array  = [1, 2, 3, 4, 5];
$array2 = ['username' => 'YamiOdymel', 
           'password' => '2016 Spring'];

arrayslicemapinterface

你他妈的我到底看了三洨,首先你要知道Golang是个强型别语言,意思是你的阵列中只能有一种型态,什么意思?当你决定这个阵列是用来摆放字串资料的时候,你就只能在里面放字串。没有数值、没有布林值,就像你没有女朋友一样。

阵列-Array

一个存放固定长度的阵列。

先撇开PHP 的「万能阵列」不管,Golang 中的阵列既单纯却又十分脑残,在定义一个阵列的时候,你必须给他一个长度还有其内容存放的资料型态,你的阵列内容不一定要填满其长度,但是你的阵列内容不能超过你当初定义的长度。

PHP

$a = ["foo", "bar"];

echo $a[0]; // 輸出:foo  

Golang

var a [2]string

a[0] = "foo"  
a[1] = "bar"

fmt.Println(a[0]) // 輸出:foo  

切片-Slice

可供「裁切」而且供自由扩展的阵列。

切片⋯⋯这听起来也许很奇怪,但是你确实可以「切」他,让我们先谈谈「切片」比起「阵列」要好在哪里:「你不用定义其最大长度,而且你可以直接赋予值」,没了。

PHP

$a = ["foo", "bar"];

echo $a[0]; // 輸出:foo  

Golang

a := []string{"foo", "bar"}

fmt.Println(a[0]) // 輸出:foo  

array_slice()slice[開始:結束]

Golang

p := []int{1, 2, 3, 4, 5, 6}

fmt.Println(p[0:1]) // 輸出:[1]  
fmt.Println(p[1:1]) // 輸出:[]  (!注意這跟 PHP 不一樣!)  
fmt.Println(p[1:])  // 輸出:[2, 3, 4, 5, 6]  
fmt.Println(p[:1])  // 輸出:[1]  

array_slice()

PHP

$p = [1, 2, 3, 4, 5, 6];

echo array_slice($p, 0, 1); // 輸出:[1]  
echo array_slice($p, 1, 1); // 輸出:[2]  
echo array_slice($p, 1);    // 輸出:[2, 3, 4, 5, 6]  
echo array_slice($p, 0, 1); // 輸出:[1]  

映照-Map

有键名和键值的阵列。

你可以把「映照」看成是一个有键名和键值的阵列,但是记住:「你需要事先定义其键名、键值的资料型态」,这仍限制你没办法在映照中存放多种不同型态的资料。

PHP

$data["username"] = "YamiOdymel";
$data["password"] = "2016 Spring";

echo $data["username"]; // 輸出:YamiOdymel  

Golang

make()map
data := make(map[string]string)

data["username"] = "YamiOdymel"  
data["password"] = "2016 Spring"

fmt.Println(data["username"]) // 輸出:YamiOdymel  

接口-Interface

终于;一个可存放多种资料型态的阵列,但难以捉模(干)。

也许你不喜欢「接口」这个词,但用「介面」我怕会误导大众,所以,是的,接下来我会继续称其为「接口」。还记得你可以在PHP 的关联阵列里面存放任何型态的资料吗,像下面这样?

PHP

$mixedData  = ["foobar", 123456];
$mixedData2 = ['username' => 'YamiOdymel', 
               'time'     => 123456];

Golang

interface{}
mixedData := []interface{}{"foobar", 123456}

mixedData2 := make(map[string]interface{})  
mixedData2["username"] = "YamiOdymel"  
mixedData2["time"]     = 123456  

不定值-Mixed Type

有时候你也许会有个不定值的变数,在PHP 里你可以直接将一个变数定义成字串、数值、空值、就像你那变心的女友一样随时都在变。

PHP

$mixed = 123;
echo $mixed; // 輸出:123

$mixed = 'Moon, Dalan!';
echo $mixed; // 輸出:Moon, Dalan!

$mixed = ['A', 'B', 'C'];
echo $mixed; // 輸出:['A', 'B', 'C']  

Golang

interface{}
var mixed interface{}

mixed = 123  
fmt.Println(mixed) // 輸出:123

mixed = "Moon, Dalan!"  
fmt.Println(mixed) // 輸出:Moon, Dalan!

mixed = []string{"A", "B", "C"}  
fmt.Println(mixed) // 輸出:["A", "B", "C"]  

逆向处理-Defer

当我们程式中不需要继续使用到某个资源或是发生错误的时候,我们索性会将其关闭或是抛弃来节省资源开销,例如PHP 里的读取档案:

PHP

$handle = fopen('example.txt', 'r');

if($errorA)  
    errorHandlerA();

if($errorB)  
    errorHandlerB();

fclose($handle); // 關閉檔案  

Golang

defer
deferdeferA->B->C->DD->C->B->A
handle := file.Open("example.txt")  
defer file.Close() // 關閉檔案但「推遲執行」,所有程式結束後才會執行這裡

if errorA {  
    errorHandlerA()
}
if errorB {  
    errorHandlerB()
}

跳往-Goto

这东西很邪恶,不是吗?又不是在写BASIC,不过也许有时候你会在PHP 用上呢。但是拜托,不要。

PHP

goto a;  
echo 'foo';

a:  
echo 'bar'; // 輸出:bar  

Golang

goto a  
fmt.Println("foo")

a:  
fmt.Println("bar") // 輸出:bar  

回圈-Loops
forforeachwhileforfor

PHP

for($i = 0; $i < 3; $i++)  
    echo $i; // 輸出:012

$j = 0;
for($j; $j < 5; $j++)  
    echo $j; // 輸出:01234

Golang

ii := 0
for i := 0; i < 3; i++ {  
    fmt.Println(i) // 輸出 012
}

j := 0  
for ; j < 5 ; j++ {  
    fmt.Println(j) // 輸出:01234
}

每个-Foreach

foreach()

PHP

$data = ['a', 'b', 'c'];

foreach($data as $index => $value)  
    echo $index . $value . '|' ; // 輸出:0a|1b|2c|

foreach($data as $index => $value)  
    echo $index . '|' ; // 輸出:0|1|2|

foreach($data as $value)  
    echo $value . '|' ; // 輸出:a|b|c|

Golang

for()rangeforeach
data := []string{"a", "b", "c"}

for index, value := range data {  
    fmt.Printf("%d%s|", index, value)  // 輸出:0a|1b|2c|
}

for index := range data {  
    fmt.Printf("%d|", index)  // 輸出:0|1|2|
}

for _, value := range data {  
    fmt.Printf("%s|", value)  // 輸出:a|b|c|
}

重复-While

while(條件)條件false

PHP

$i = 0;

while( $i < 3 ) {  
    $i++;
    echo $i; // 輸出:123
}

while(true)  
    echo "WOW" // 輸出:WOWWOWWOWWOWWOW...

Golang

forfor;for
i := 0

for i < 3 {  
    i++
    fmt.Println(i) // 輸出:123
}

for {  
    fmt.Println("WOW") // 輸出:WOWWOWWOWWOWWOW...
}

做.. 重复-Do While

do .. while()

PHP

$i = 0;

do {  
    $i++;
    echo $i; // 輸出:123
} while($i < 3);

Golang

for
i := 0

for {  
    i++
    fmt.Println(i) // 輸出:123

    // 注意這個條件式和 PHP 有所不同
    if i > 2 {
        break
    }
}

Golang

goto
i := 0

LOOP:  
    i++
    fmt.Println(i) // 輸出:123

    if i < 3 {
        goto LOOP
    }

日期-Date
date()

PHP

echo date("Y-m-d H:i:s"); // 輸出:2016-07-13 12:59:59  

Golang

Y-m-d1231
fmt.Println(time.Now().Format("2006-2-1 03:04:00"))          // 輸出:2016-07-13 12:59:59  
fmt.Println(time.Now().Format("Mon, Jan 2, 2006 at 3:04pm")) // 輸出: Mon, Jul 13, 2016 at 12:59pm  

切割字串-Split
explode()die()

PHP

$data  = 'a, b, c, d';
$array = explode(', ', $data);

Golang

简单的就让一个字串给「爆炸」了,那么Golang 呢?

data  := "a, b, c, d"  
array := strings.Split(data, ", ")  

strings
关联阵列-Associative Array

这真的是很常用到的功能,就像物件一样有着键名和键值,在PHP 里面你很简单的就能靠阵列(Array)办到。

PHP

$data = ['username' => 'YamiOdymel',
         'password' => '2016 Spring'];

echo $data["username"]; // 輸出:YamiOdymel  

Golang

map
data := map[string]string{  
           "username": "YamiOdymel", 
           "password": "2016 Spring"}

fmt.Println(data["username"]) // 輸出:YamiOdymel  

是否存在-Isset
isset()

PHP

// 如果 $data['username'] 存在
if(isset($data['username'])) {  
    $username = $data['username'];
}

Golang

map
username, exists := data["username"]

if !exists {  
    fmt.Printf("你要找的資料不存在。")
}

指针-Pointer
A = 1; B = A;BABA

指针比起复制一个变数,他会建立一个指向到某个变数的记忆体位置,这也就是为什么你改变指针,实际上是在改变某个变数。

PHP

function zero(&$number) { // & 即是指針  
    $number = 0;
}

$A = 5;
zero($A);

echo $A; // 輸出:0  

Golang

*&
func zero(number *int) {  
    number = 0
}

func main() {  
    A := 5;
    zero(&A)

    fmt.Printf("%d", A) // 輸出:0
}

错误处理-Error Exception

有些时候你会回传一个阵列,这个阵列里面可能有资料还有错误代号,而你会用条件式判断错误代号是否非空值。

PHP

function foo($number) {  
    if($number !== 1)
        return ['number' => -1, 
                'error'  => '$number is not 1'];

    return ['number' => $number, 
            'error'  => null];
}

$bar = foo(0);

if($bar['error'])  
    echo $bar['number'], $bar['error']; // 輸出:-1
                                        //      $number is not 1

Golang

errorerrors
try .. catchtry
import "errors"

func foo(number int) (int, error) {  
    if number != 1 {
        return -1, errors.New("$number is not 1")
    }
    return number, nil
}

if bar, err := foo(0); err != nil {  
    fmt.Println(bar, err) // 輸出:-1
                          //      $number is not 1
}

ifif

抛出和捕捉异常-Try & Catch

try .. catchtry

PHP

function foo($number) {  
    if($number < 10)
        throw new Exception('$number is less than 10');
    else if($number > 10)
        throw new Exception('$number is greater than 10');
}

try {  
    foo(9);
} catch(Exception $e) {
    echo $e->getMessage(); // 輸出:$number is less than 10
}

try {  
    foo(11);
} catch(Exception $e) {
    echo $e->getMessage(); // 輸出:$number is greater than 10
}

Golang

try .. catchpanic()recover()defer
panic()throwexit()panic()deferdeferpanic()
deferpanic()deferdeferrecover()
recover()catchdeferrecover()panic()
// 建立一個模仿 try&catch 的函式供稍後使用
func try(fn func(), handler func(interface{})) {  
    // 這不會馬上被執行,但當 panic 被執行就會結束程式,結束程式就必定會呼叫 defer
    defer func() { 
        // 透過 recover 來從 panic 狀態中恢復,並呼叫捕捉函式
        if err := recover(); err != nil {
            handler(err)
        }
    }()
    // 執行可能帶有 panic 的程式
    fn()
}

func foo(number int) {  
    if number < 10 {
        panic("number is less than 10")
    }
    if number > 10 {
        panic("number is greater than 10")
    }
}

func main() {  
    try(func() {
        foo(9)
    }, func(e interface{}) {
        fmt.Println(e) // 輸出:number is less than 10
    })

    try(func() {
        foo(11)
    }, func(e interface{}) {
        fmt.Println(e) // 輸出:number is greater than 10
    })
}

套件/汇入/汇出-Package / Import / Export
require()include()

PHP

// a.php
<?php  
    $foo = "bar";
?>

// index.php
<?php  
    include "a.php";

    echo $foo; // 輸出:bar
?>

Include Hell

Golang

// a.go
package main

var foo string = "bar"  

// main.go
package main

import "fmt"

func main() {  
    fmt.Println(foo) // 輸出:bar
}

main.gofmta.go

蛤???杀小??????」你仿佛回到了几秒钟前的自己。

main.gofoomain

套件-Package

.go
package main  

main

PHP

// a.php
<?php  
    function foo() {
        // ...
    }
?>

// index.php
<?php  
    include "a.php";

    foo();
?>

Golang

接着是Golang;注意!你不需要引用任何档案,因为下列两个档案同属一个套件。

// a.go
package main

func foo() {  
    // ...
}

// main.go
package main

func main() {  
    foo()
}

include()require()

汇入-Import

在Golang 中没有引用单独档案的方式,你必须汇入一整个套件,而且你要记住:「一定你汇入了,你就一定要使用它」,像下面这样。

package main

import (  
    "fmt"                           // 引用底層套件
    "time"                          // 這也是底層套件
    "github.com/yamiodymel/teameow" // 來自 Github 的 "teameow" 套件
)

func main() {  
    // 然後像下面這樣使用你剛匯入的套件
    fmt.XXX()
    time.XXX()
    teameow.XXX()
}

main()_
import (  
    _ "fmt"
)

如果你的套件出现了名称冲突,你可以在套件来源前面给他一个新的名称。

import (  
    "github.com/karisu/teameow"
    neko "github.com/yamiodymel/teameow"
)

func main() {  
    teameow.XXX()
    neko.XXX()
}

汇出-Export

现在你知道可以汇入套件了,那么什么是「汇出」?同个套件内的函式还有共享变数确实可以直接用,但那并不表示可以给其他套件使用,其方法取决于函式/变数的「开头大小写」

是的。Golang依照一个函式/变数的开头大小写决定这个东西是否可供「汇出」

// a.go
package hello

// 注意:這裡的 Foo 的開頭字母是大寫!
var Foo string = "bar"

// 注意:這個 World 函式的開頭字母是大寫!
func World() {  
    // ...
}

// b.go
package test

import (  
    "hello"
    "fmt"
)

func main() {  
    fmt.Println(hello.Foo) // 輸出:bar

    hello.World()
}

这用在区别函式的时候格外有用,因为小写开头的任何事物都是不供汇出的,反之,大写开头的任何事物都是用来汇出供其他套件使用的。

publicprivateprotected
类别-Class

在Golang 中没有类别,但有所谓的「建构体(Struct)」和「接口(Interface)」,这就能够满足几乎所有的需求了,这也是为什么我认为Golang 很简洁却又很强大的原因。

让我们先用PHP 建立一个类别,然后看看Golang 怎么解决这个问题。

PHP

class Foobar {  
    public $a = "hello, world";

    public function test() {
        echo $this->a;
    }
}

$b = new Foobar();
$b->test(); // 輸出:hello, world!

Golang

publicprivateprotected
// 先定義一個 Foobar 建構體,然後有個叫做 a 的字串成員
type Foobar struct {  
    a string
}

// 定義一個屬於 Foobar 的 test 方法
func (f *Foobar) test () {  
    // 接收來自 Foobar 的 a(等同於 PHP 的 `$this->a`)
    fmt.Println(f.a)
}

b := &Foobar{a: "hello, world!"}  
b.test() // 輸出:hello, world!  

建构子-Constructor

new__construct()

PHP

class Test{  
    public $a;

    function __construct() {
        $this->a = "foobar";
    }

    function show() {
        echo $this->a;
    }
}

$b = new Test();
$b->show(); // 輸出:foobar

Golang

但是在Golang 里因为没有类别,也就没有建构子,不巧的是建构体本身也不带有建构子的特性,这个时候你只能自己在外部建立一个建构用函式。

type Test struct {  
    a string
}

func (t *Test) show() {  
    fmt.Println(t.a)
}

// 用來建構 Test 的假建構子
func newTest() (test *Test) {  
    test = &Test{a: "foobar"}

    // 這裡會回傳一個型態是 *Test 建構體的 test 變數
    return
}

b := newTest()  
b.show() // 輸出:foobar  

嵌入-Embed

让我们假设你有两个类别,你会把其中一个类别传入到另一个类别里面使用,废话不多说!先上个PHP 范例(为了简短篇幅我省去了换行)。

PHP

class Foo {  
    public $msg = "Hello, world!";
}

class Bar {  
    public $foo;

    function __construct($foo){ $this->foo = $foo;    }
    function show()           { echo $this->foo->msg; }
}

$a = new Foo();
$b = new Bar($a);
$b->show(); // 輸出:Hello, world!

Golang

在Golang中你也有相同的用法,但是请记得:「任何东西都是在「类别」外完成建构的」。

type Foo struct {  
    msg string
}

type Bar struct {  
    *Foo
}

func (b *Bar) show() {  
    // Foo 中的 msg 會直接暴露在 Bar 底下
    // 所以你可以直接使用 b.msg
    fmt.Println(b.msg)
}

a := &Foo{msg: "Hello, world!"}  
b := &Bar{a}  
b.show() // 輸出 Hello, world!  

遮蔽-Shadowing

在PHP 中没有相关的范例,这部分会以刚才「嵌入」章节中的Golang 范例作为解说对象。

FooBarFooBar

这个时候被嵌入的成员就会被「遮蔽」,下面是个实际范例,还有你如何解决遮蔽问题:

type Foo struct {  
    msg string
}

type Bar struct {  
    *Foo
    msg string // 遮蔽了 Foo 的 msg
}

a := &Foo{msg: "Hello, world!"}  
b := &Bar{Foo: a, msg: "Moon, Dalan!"}

fmt.Println(b.msg)     // 輸出:Moon, Dalan!  
fmt.Println(b.Foo.msg) // 輸出:Hello, world!  

多形-Polymorphism

虽然都是呼叫同一个函式,但是这个函式可以针对不同的资料来源做出不同的举动,这就是多形。你也能够把这看作是:「讯息的意义由接收者定义,而不是传送者」。

目前PHP 中没有真正的「多形」,不过你仍可以做出同样的东西。

PHP

class Foo{ public $msg = "hello";  }  
class Bar{ public $msg = "world!"; }

class Handler {  
    public function process($class) {
        switch(get_class($class)) {
            // 依照不同的資料類型做出不同的舉動
            case 'Foo':
                echo '處理 Foo | ' . $class->msg . ', world!';
                break;

            case 'Bar':
                echo '處理 Bar | ' . 'hello, ' . $class->msg;
                break;
        }
    }
}

$foo = new Foo();
$bar = new Bar();
$handler = new Handler();

// 雖然都是同個函式,但是可以處理不同資料
$handler->process($foo); // 輸出:處理 Foo | hello, world!
$handler->process($bar); // 輸出:處理 Bar | hello, world!

Golang

interface
type Foo struct {  
    msg string
}

type Bar struct {  
    msg string
}

// 透過 Handler 實作 process
type Handler interface {  
    process()
}

// 處理 Foo 資料的 process
func (f Foo) process() {  
    fmt.Printf("處理 Foo | %s, world!", f.msg)
}

// 處理 Bar 資料的 process
func (b Bar) process() {  
    fmt.Printf("處理 Bar | hello, %s", b.msg)
}

foo := Foo{msg: "hello"}  
bar := Bar{msg: "world!"}

// 雖然都是同個函式,但是可以處理不同資料
Handler.process(foo) // 輸出:處理 Foo | hello, world!  
Handler.process(bar) // 輸出:處理 Bar | hello, world!  

如果你对Interface还不熟悉,可以试着查看「解释Golang中的Interface到底是什么」文章。

谢谢你看到这里,可惜这篇文章却没有说出Golang 最重要的卖点:「Goroutine」和「Channel」