Handbook of PHP To Go Transition (II)

Keywords: PHP Go MySQL

Preface

As a PHP programmer, I feel honored. But in the constant changes of the times, we must have enough knowledge to survive.

Let's start with Go Linguistics. Somehow, I always feel that PHP and Go have many similarities.

Hope to see this article, you can have a basic understanding of Go. This series of articles describes how I learn the Go language. To distinguish between PHP code and Go code.

Load

PHP

namespace Action
use Action

Go

package Action
import "action"

array

PHP

// Initialization
$arr = []
$arr = array()
// Initial assignment
$arr = [1,2,3]
// Multidimensional array
$arr = [][]
// Get value
echo $arr[1]
// Get the total number of arrays
echo length($arr)
// Getting Array Interval
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
// Set key=> value
$arr = ["username"=>"zhangsan","age"=>13]
// Delete the specified subscript
unset($arr[0])

Go Array & Slice (Slice is a View of an array, just like MySQL's view)

// Initialization
var arr [5]int
// Initial assignment
arr := [5]int{1, 2, 3, 4, 5}
// No need to declare the number of arrays
arr := [...]int{1, 2, 3, 4, 5, 6, 7}
// Multidimensional array
var arr [4][5]bool
// Get value
fmt.Println(arr[1])
// Get the total number of arrays
fmt.Println(len(arr))
// Obtaining the intervals of arrays is obvious, and the operation of Go s on arrays is more convenient and intuitive.
a := [...]string{"red","green","blue","yellow","brown"}
fmt.Println(a[1:2])
// Setting key=> value requires Map here
m := map[string]string{
    "username": "zhangsan",
    "age" : "13"
}
// System method for deleting specified subscript Go without deleting array subscript
arr := arr[1:]
// Deleting the subscripts in the middle position can remove the specified subscripts by merging them.
arr := append(arr[:3],arr[4:])

Cyclic structure

PHP

// Basic structure
for($i=0;$i<10;$i++){
    echo $i;
}
// Dead cycle
for($i=0;$i<10;$i++){
    echo $i;
    $i--
}
// Get key,value
foreach($arr as $key=>$value){
    echo $key,$value
}

Go

// Basic structure
for i := 0; i < 10; i++ {
    fmt.Println(i)
}
// Dead loops make it very convenient to write Go Dead loops.
for {
    fmt.Println("")
}
// Get key,value
for k, v := range arr {
    fmt.Println(k, v)
}

control structure

PHP

// if
if(true){

}
// switch
switch(true){
    case true:
        echo true;
        break;
}

Go

// if
if true {
        
}
// switch Go Language Switch Case does not need break
switch true {
    case true:
        fmt.Println(true)
}

class

PHP

// Declare a class
class City{}

Go

// Declaring a structure here is not confusing to the public, because Go itself does not have the concept of a class, but its declaration and operation are similar to the concept of a class.
type City struct{}

The Structural Experience of Go Language will be compared in the next chapter.

Thank

Thank you for reading here. I hope this article can help you. Thank you

Posted by ++Sti++ on Wed, 23 Jan 2019 09:42:13 -0800