PHP to Go shift

  • This article is just a bit of personal learning experience. If there are any mistakes in the expression, please correct me.
  • More and more friends are switching from PHP language to Go language learning. Then novices will encounter many problems and bottlenecks when switching to go.
  • This is mainly because many friends don’t know where to start learning this new language. And most of them will learn with some previous PHP thinking.
  • I have summarized some common problems and combined them with my own open source project IrisAdminApi to elaborate on these problems. I hope it can be of some help to beginners of go.
  • What is the difference between GO language and PHP?
  • How to start a GO program?
  • Why do we need to use hot compilation tools during development? What are hot compilation tools?
  • Why does it still prompt command not found after the hot compilation tool is installed?
  • Why does the startup program prompt that the method or variable does not exist? But can it run normally using hot compilation tools?
  • How to use IDE, such as Goland, to develop GO programs?
  • How to deploy a GO project?

What is the difference between GO language and PHP?

  • The life cycle of PHP scripts and the operation of fpm (FastCGI Process Manager),
  • As you can see from the picture below, php is a dynamic language, and the php script will be executed once for each request. *As shown below:

Minion

  • However, the go language is completely different. It is a static language. Every time the go script is modified, it must be recompiled to take effect.
  • The go language usually has two commands to compile scripts, go run main.go and go build main.go.
  • go build main.go: will generate a main executable file in the current directory, and the windows system will generate the main.exe executable file.
  • go run main.go: The script will be executed at the same time as the script is compiled, and the executable file will not be generated.
  • Note: You need to recompile the script every time you modify it. No matter you execute go run main.go or go build main.go, your modification will take effect.
  • Of course, there are other differences between go run main.go and go build main.go. Friends who are interested can search on their own.

How to start a GO program?

  • You can start a go program through go run main.go.
  • You can also execute ./main directly on the command line, double-click main.exe on Windows system.
  • Hot compilation tools are usually used during development: air, gowatch, etc.

Why do we need to use hot compilation tools during development? What are hot compilation tools?

  • The hot compilation tool can automatically help you execute the compilation command and execute the compiled executable file after you modify the code when you develop a go script program.
  • The principle of the hot compilation tool is very simple, which is to start a coroutine to monitor your project files in real time. After monitoring that the project file has been modified, execute the relevant commands.
Why does the prompt command not found appear after the hot compilation tool is installed?
  • This problem is very simple. In fact, the executed command does not exist, or it exists but the system cannot find it.
  • No matter what system it is, there will be a mechanism for finding executable commands. This is usually achieved by setting system variables.
  • The specific how to set the system environment variables of each system will not be introduced here.
  • Here is a brief introduction on how to troubleshoot the go tools you have installed, such as command not found problems when executing commands such as gowatch and air.
  • First: Check whether there is a corresponding executable file in the gopath/bin directory, such as air, which is the air.exe file under win system. If there is no indication that the installation was not successful, you need to reinstall it.
  • The second situation: If the file already exists in the gopath/bin directory, command not found still occurs. Then it means that your gopath/bin directory has not been added to the system environment variables. There are two solutions at this time. The first one: directly copy the executable file to the goroot/bin directory. The second method: of course, is to add the gopath/bin directory to the system environment variables.
  • Rare cases: Modifying system variables does not take effect. For example, if the system is not restarted under the Windows system or the terminal is not restarted under the Linux system, the set system variables may not take effect.
Why does the startup program prompt that the method or variable does not exist? But can it run normally using hot compilation tools?
  • Many document introductions start the go script program through go run main.go. Because most of the documented go script example programs have only one file main.go.
  • When you encounter some projects with many files, such as IrisAdminApi, you will find that using go run main.go will prompt an error that some methods do not exist. However, the program can be started normally using the air hot compilation tool.
  • The reason for this is that under the package name package main, there are actually two files main.go and bindata.go. So you need to execute go run main.go bindata.go to start the program normally. You must have discovered the principle that you can add multiple files after go run.

How to use IDE, such as Goland, to develop GO programs?

  • Using goland for development mainly encounters a pitfall: the go environment of each project in it is isolated and does not share the settings in go env.
  • Therefore, when creating a new project, you need to set some parameters of the go environment.
  • Mainly GOROOT,GOPATH,Go Modules. These parameters Go Modules are used to set dependent agents.

image.png

How to deploy a GO project?

  • How to deploy a go project to the server? Take the IrisAdminApi project as an example:
  • Start the compiled file directly and deploy it through process daemon tools such as Supervisor and pm2. This method is inconvenient for upgrade and maintenance, and the deployment is also relatively complicated. (not recommended)
  • Deploy via docker and package the program into a docker image. Then start the program through docker. This method is convenient for upgrade and maintenance, and is suitable for programs that often require version iteration.
  • Directly execute the compiled file

go get… When adding private warehouse dependencies, an error is reported (go 1.13+ version)

  • Set GOPRIVATE variable
export GOPRIVATE=gitlab.com/xxx