Preface
As a PHP programmer, I feel honored. However, in the changing times, one must have sufficient knowledge to survive.
Let’s start with Go linguistics.
I hope you can have a basic understanding of Go after reading this article. This series of articles describes the way I learned Go language. The comparison between PHP code and Go code is used to distinguish and understand.
Load
PHP
namespace Action
use Action
Go
package Action
import "action"
array
PHP
//initialization
$arr = []
$arr = array()
//Initialize assignment
$arr = [1,2,3]
//Multidimensional Array
$arr = [][]
//Get Value
echo $arr[1]
//Get the total number of arrays
echo length($arr)
//Get 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 array, just like MySQL view)
//initialization
var arr [5]int
//Initialize assignment
arr := [5]int{1, 2, 3, 4, 5}
//There is 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))
//It is obvious to obtain the array interval, and Go is more convenient and intuitive to operate the array.
a := [...]string{"red","green","blue","yellow","brown"}
fmt.Println(a[1:2])
//set key=>value Map is required here
m := map[string]string{
"username": "zhangsan",
"age" : "13"
}
//delete specified subscript Go there is no system method to delete array subscript
arr := arr[1:]
//Delete the subscript at the middle position to remove the specified subscript by merging.
arr := append(arr[:3],arr[4:])
loop 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 Cycle Visible Go Write Dead Cycle Very Convenient
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 Case of switchgo language 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 the public, because Go itself does not have the concept of class, but its declaration and operation methods are similar to the concept of class.
type City struct{}
The structure of Go language will be compared in the next chapter.
Thank you
Thank you for seeing here. I hope this article can help you. Thank you