Home > Backend Development > Golang > Share some introductory Go knowledge for PHPer

Share some introductory Go knowledge for PHPer

藏色散人
Release: 2020-08-19 13:19:01
forward
2660 people have browsed it

The following column will share with you some introductory Share some introductory Go knowledge for PHPer knowledge for PHPer from the Share some introductory Go knowledge for PHPerlang Language Tutorial column. I hope it will be helpful to friends in need!

Share some introductory Go knowledge for PHPer

#Recently, I started developing an internal command line interface application for my work team. My main programming language of choice is PHP, but I wanted to create the program in a language that would run on any platform without having to install an interpreter. I would also like the application to be self-contained in a binary file for easier distribution and installation. I downloaded Share some introductory Go knowledge for PHPer and was amazed at how easy it was to learn and how productive I got in a short time. Share some introductory Go knowledge for PHPer's procedural programming model really suited our PHP developer mindset, and I was able to get the application up and running quickly. There are some obvious differences with PHP though, so I wanted to share those with other PHP developers who want to learn Share some introductory Go knowledge for PHPer.

Recommended: "golang tutorial"

MacOS

I am using a Mac, so I installed go through Homebrew: brew install go . If you don’t have Homebrew installed on your Mac, I highly recommend it.

Windows & Linux

Share some introductory Go knowledge for PHPer's official website has download resources for each operating system, including the "Next Next" installer for Windows systems.

Start

In Share some introductory Go knowledge for PHPer, the source code of all projects is saved in one directory: GOPATH. By default, GOPATH is set under go in the home directory, such as /Users/andrewdavis/go. There are bin directory and src directory under GOPATH. The bin directory stores the binary form of dependencies you downloaded. You can add the bin directory to your PATH environment variable. You can edit the file .bashrc/.zshrc in the terminal and add export PATH=$PATH:$(go env GOPATH)/bin. To learn Share some introductory Go knowledge for PHPerlang, you can run the command go get golang.org/x/tour/gotour in the terminal to download the Share some introductory Go knowledge for PHPer programming guide. go get will download the source code of the third-party dependency specified in the parameters. and binary files. Now you can run gotour in the terminal and it will start a web server and automatically open browser access.

To create a project, create a directory under src in GOPATH: mkdir -p $GOPATH/src/helloworld. Open this folder cd $GOPATH/src/helloworld and create a file named main, touch main.go. In this file, enter the following:

package main

import "fmt"

func main() {
  fmt.Println("Hello world!")
}
Copy after login

The starting point for all Share some introductory Go knowledge for PHPer programs is the main function in the main package. Next, you can run go run main.go to run the program. You can also run go install and the program will be compiled and placed in the bin directory so you can execute helloworld in the terminal and it will run you code.

Main Differences from PHP

Now that you have a project set up, you can start exploring the different go features. The first thing you'll notice is that semicolons are not required in Share some introductory Go knowledge for PHPer. The end of a statement is detected by a new line. Here are some more differences that took me some time to understand:

Variables

Share some introductory Go knowledge for PHPer is a static and strongly typed language, so every variable has an assignment Give it the type. Variables in the function are assigned using :=, which automatically assigns the type to the variable: name := "Andrew" // name is now a string . To create A variable that does not set any data or to create a variable outside a function, you must use the var keyword: var name string.

If Statement

The if statement works the same way as in PHP, but it does not require parentheses to surround the condition. This difference initially made me think It's confusing when reading the code in Share some introductory Go knowledge for PHPer. However, I think this makes the code more readable.

package main

import "fmt"

func main() {
  value := false
  if value {
    fmt.Println("Value is true")
  } else {
    fmt.Println("Value is false")
  }
}
Copy after login

Packages vs Namespaces

Share some introductory Go knowledge for PHPer 使用术语 package 来为其内容创建命名空间。如果你的 Share some introductory Go knowledge for PHPer 代码中有一个名为 controllers 的文件夹,在这个文件夹下的所有文件都需要以 package controllers 开头。想要在其他包内导入控制器,你需要使用 import "helloworld/controllers"。包内任何以大写字母开头的名称都可以用在其他包中。 如果在 controllers 中有一个名为func HelloWorld () 的函数,你可以在 controllers 导入之后使用 controllers.HelloWorld() 来执行这个函数。任何以小写字母开头的名称只能在包内使用。不需要 privatepublic 进行声明。

Strings

在 Share some introductory Go knowledge for PHPer 中,所有字符串必须用双引号包围。Share some introductory Go knowledge for PHPer 中的一个单引号值代表一个 rune (一个 Unicode字符)。习惯性的,我会使用单引号声明字符串,因为这是 PHP 中的常见做法。使用双引号声明字符串会需要一段时间调整。

var name = "Andrew"
var copy = '©'
Copy after login

Structs vs Classes

Share some introductory Go knowledge for PHPer 没有像 PHP 的 class 语法. 取而代之的, 它使用结构体对自定义数据结构建模. 你可以像下面这样声明一个结构体:

package main

type Cup struct {
  name string
  color string
  volume int
}
Copy after login

你可以通过在函数名称之前引用结构体来为该结构体添加方法。

func (c Cup) nameAndColor() string {
  return c.name + ": " + c.color
}
Copy after login

然后您可以通过在结构体名称后用大括号传递其初始值来实例化一个结构体. 使用 . 来调用结构体的方法.

func main() {
  c := Cup{name: "Solo", color: "Red", volume: 12}
  c.nameAndColor() // 返回 "Solo: Red"
}
Copy after login

若要创建一个方法修改结构体实例, 该方法必须引用指向结构体的指针:

func (c *Cup) crush() {
  c.volume = 0
}
Copy after login

错误

在 Share some introductory Go knowledge for PHPer 中,错误并不会视为异常。没有 trycatch 的机制。作为替代,如果发生错误,需要从函数内返回错误。Share some introductory Go knowledge for PHPer 支持一个函数返回多个值。如果调用的函数可能会返回错误,你必须检测这个错误是否存在,然后处理这个错误。

package main

import "fmt"

func GetName(name string) (string, error) {
  if name == "Bob" {
    return "", fmt.Errorf("Name cannot be Bob")
  }

  return name, nil
}

func main() {
  name, err := GetName("Bob")
  if err != nil {
    fmt.Println("Uh-oh an error has occurred")
  }
}
Copy after login

Final

当然,Share some introductory Go knowledge for PHPer 还有很多东西要学,但是希望这能帮助你更好的开始。学习 Share some introductory Go knowledge for PHPer 的很好的资源有很多。对我最有帮助的是 Share some introductory Go knowledge for PHPer 文档 和 Share some introductory Go knowledge for PHPer By Example。如果你有任何想法或问题,请留下评论。 谢谢阅读!

原文地址:https://dev.to/restoreddev/introduction-to-go-for-php-developers-33ko

译文地址:https://learnku.com/go/t/46321

The above is the detailed content of Share some introductory Go knowledge for PHPer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template