Some PHPers will consider switching to Go or Java. Let’s make a simple comparison of these two languages. This article is for reference only!
Go language
Compared to Java, Go language is compiled into machine code and then run directly. Much like the C language. Because it does not have a virtual machine, this is very different from Java. It is object-oriented, and to a certain extent, it is not just a new C language with automatic garbage collection. From a Java programmer's perspective, there are things that are so different that learning Go language becomes extremely challenging and can help with a deeper understanding of programming language constructs, along with objects, classes and all other What's going on with language components, even with Java.
What I mean is that if you understand the object-oriented implementation in the Go language, then you will also understand the different implementations of these things in the Java language.
Whether to use garbage collection
The memory management part is crucial in programming languages. Assembly language lets you do everything, or rather, it requires you to do everything. The C standard library provides some basic functions for memory management, but this still requires you to manually release them after calling malloc to allocate memory. Automated memory management techniques emerged with C, Python, Swift, and Java. Go language is also one of them.
For Java and other JVM languages (including Python's JVM implementation), memory is managed by the JVM. The JVM has a very mature garbage collection. The garbage collection is always running in one or more threads - in parallel with the worker threads or sometimes suspending these worker threads to mark those unreachable objects, clear them and scatter the detected ones. The memory is compressed together. All you need to worry about is performance.
The Go language also uses this method, but there are still some subtle differences. Go language does not have references, but it does have pointers. This difference is very important. The Go language can be integrated with additional C code. For performance reasons, there is nothing registered at runtime like references. The actual pointer is transparent to the running system. The allocated memory can still be analyzed to collect reachability information and useless objects can still be marked and cleared, but the memory cannot be moved elsewhere for compaction.
The Go language has garbage collection but it is not a complete GC like Java because it does not compress the memory. This isn't necessarily a bad thing. Because this makes sense for the server to run for a long time without causing memory fragmentation. Some JVM garbage collectors will also skip the compaction step in order to reduce GC pauses when clearing the old generation, and only use compaction as a last resort. There is no implementation of this last resort step in Go, which may cause some problems in some rare scenarios. When you learn this language, you are unlikely to encounter this problem.
Local variables
In the Java language, local variables (and sometimes objects in newer versions) are stored on the stack. This is also true in C, C++, and other languages that implement the call stack itself. Go is no exception, unless you simply return a pointer to a local variable from a function. This is a fatal error in C language. For Go, the Go compiler finds that the allocated "object" (I will explain why quotes are used here later) escapes from the method and allocates it (memory) accordingly, so this "object" is in the function It is still alive after return and the pointer to it does not point to obsolete memory with unreliable data.
Closure
You can write a function inside a function and then return this function, just like a functional language (Go language is a functional language) , then the local variable is a closure variable in this function.
Function return value
Function can not only return a single value, but also return multiple values. This seems like bad practice if not used appropriately. This is true for both Python and Perl. Returning multiple values is useful in some situations. It is mainly used for return values and 'nil' or error codes. In this way, the past habit of encoding errors into the return type (usually returning -1 as the error code like a C standard library call, and other non-negative values representing other meanings) will be replaced by this more readable method.
Object-oriented
By treating closures and functions as first-class citizens, Go can achieve at least the same object-orientation as JavaScript. But in fact Go has interfaces and structures and can do more. However, interfaces and structures are not real classes, they are value types. They are passed by value, and no matter where they are stored in memory, the data they store is just pure data, without an object header or anything like an object header. Structs in Go are very similar to structs in C - they can both contain fields, but they cannot inherit from each other or contain methods. Object-oriented implementation is slightly different.
You can specify the structure when you define the method itself, rather than stuffing the method into the class definition. Structures can also contain other structures. If the field you want to reference does not have a name, you can reference it by its type, which will also implicitly become the name of the field. Or as long as this field or method belongs to the top-level structure, you can reference it
When you want to specify the structure that can call the method, you can specify it yourself through the structure or point to a pointer to the structure. If a method is applied to a struct, the method gets a copy of the calling struct (which is passed by value). If the method is applied to a pointer to a structure, then the pointer will be passed (similar to passing by reference). In the latter case, the method can also mutate the struct (in this sense, the struct is not a value type, since value types are immutable). Both can be used to meet the needs of the interface.
Go’s syntax is also more tolerant for structures and pointers. In C, you can use b.a to access the a field of a structure you own. For pointers to structures in C, you must use b->a to access the same fields. b.a will report an error when using pointers. Go thinks that writing b->a is meaningless (yes, that's what it means). Why does the -> operator make the code look cluttered when the . operator can be overloaded? If a structure can access fields in this way, then it should also be accessed in this way through pointers, which is very logical.
The type is on the variable not the object
That's why I put the quotes around "object". When Go stores a structure, it is a piece of memory. It has no object header. In fact, the variable contains the type of the value. If the variable is a structure type, we already know it at compile time. If the variable is of an interface type, then the variable will point to its value and refer to the actual type of the value.
Implementing interfaces
Interfaces are very simple and very complex in Go. An interface declares a bunch of methods that structs must implement if they want to be compatible with the interface. Inheriting interfaces is the same as inheriting structs. The strange thing is that when a struct implements an interface you don't need to specify it explicitly. In fact, it is not the interface that implements the interface, but this set of functions that uses the structure or the pointer to the structure as the receiver. If all functions of the interface are implemented, then this structure implements the interface. If some functions are not implemented, then the implementation is incomplete.
Why do we need the ‘implements’ keyword in Java but not Go? Go really doesn't need it because it is fully compiled and there is no class loader like Java that loads compiled code separately during runtime. If a struct is supposed to implement an interface but does not, this will be discovered at compile time and there is no need to explicitly indicate whether the interface implements the interface. You can implement this using Go's reflection, which will cause a runtime error, but in any case, the 'implements' keyword declaration will have no effect.
Threads and Queues
Go language has built-in threads and queues. They are called coroutines and channels. To start a coroutine, you just need to write the go function call(), which will be started in another thread. Although there are methods or functions for locking "objects" in the Go standard library, native multi-threaded programming uses channels. Channel is a built-in type in Go, which is a fixed-size first-in-first-out channel of any other type. You can push a value into the channel, and the coroutine can pull the value from the channel. If the channel is full, pushing will block; if the channel is empty, pulling will block.
There are errors but no exceptions in Go
Go actually has exception handling, but it is not used like in Java. Exceptions are called 'panics' and are used when there is some real panic in the code. From the perspective of Java rules, 'panic' is similar to some exceptions ending with '...Error'. This state is returned by a system call when there is an exception instance or an error that can be handled, and application functions are expected to handle this state in a similar pattern.
There is no finally, defer replaces it
Tightly coupled exception handling is a feature that Java implements together with the try/catch/finally feature. In Java you can put code that will be executed no matter what in a finally block. Go provides the keyword 'defer' that allows you to specify a function to be called before the method returns (even if the method panics). Using Defer to solve the above problem makes it less susceptible to abuse. You cannot write any executable code after a deferred function call. But in Java, you can even write a return statement in the finally block, or see a very confusing try statement used to handle the code to be executed in the finally block, which may also throw an exception. Go tends to the former.
In general, Go is an interesting language. It is not a replacement for Java, not even on a language level. Java and Go do not serve the same type of tasks - Java is an enterprise-level development language, and Go is a system-level programming language.
Recommended java video tutorial: JAVA video tutorial
Recommended go video tutorial: go video tutorial
The above is the detailed content of Convert php to go or java?. For more information, please follow other related articles on the PHP Chinese website!