Use the start of go

Ask me how familiar Golang is, at least for now, at the threshold level as I write this.

The reason for using Golang at present is just to understand the following as a language. I have seen that Golang can package EXE, and I want to know whether it is also possible to develop a PC software with Golang without using C WPF#. Then I googled Flutter, which is supposed to be a cross-platform development UI using DART, an ES6 + Java integration language that is also made by Google but is not covered in this discussion.

Why not use electron, PyQt5 development?

Using electron

In fact, I have not tried to use electron [hereinafter referred to as E6N]. After all, I still know JS quite well. If I get started, it should be faster. However, my computer has been stuck in Node install until I went to Baidu again recently. Say you need to set up a electron_mirror agent on taobao NPM config set electron_mirror http://npm.taobao.org/mirrors/electron/

Just downloaded it. I have also collected some good information about E6N online, but many people say that IT consumes a lot of memory and has poor performance. However, it cannot be said that JS is not an efficient language in itself. I think THE development of E6N is similar to the mixed development of APP, which is to write HTML pages and use some SDK for native interaction. When native doesn’t work, you need to write some basic interactions yourself. This is probably true of many cross-platform developments, but admittedly, it can work if you don’t need a lot of special effects. The use of E6N development should be representative of Visual Studio code, see that thunder X is also used to do UI.

Develop desktop applications in Python

For Python, I have developed a simple test tool for native use, but the layout update is not very efficient for me who is used to JS. Even though PY is known as a concise language, it is not the same as HTML for professional typesetting. As for the learning of Python, there are some algorithmic interactions, so I learned Python. When writing Python code, I had a hard time thinking about alignment formatting. I also want to get rid of all the curly braces. Learned Django a little bit, wrote an algorithm service, and then later transformed it in flask. Overall, there is not much difference in code volume compared to Node. Flask’s decorators, however, work just as well as Java. So I’m used to this annotated STYLE of API programming. Python needs to get used to this becoming self

Java development PC

Well, I did this in my early days with Java, similar to PyQt. UI debugging is hard, the native UI is ugly, and the small achievements it makes don’t mean much by themselves

#C + wpf

I thought I had learned Java and it would not be difficult to use c# as the standard language. In fact, I overestimated myself. I still need to learn all languages systematically, even if I understand them, I can’t write them. It’s a situation where everything seems to go wrong. When using VS, how to generate an EXE has been baidu for a long time.

Introduction: The emphasis is not on PC software development, but basic learning

Although much has been said about how to develop this, the focus of this chapter is really on basic usage versus the following commonly used languages

JavaScript TypeScript Java Python Go

Why do I use these? Because they all have some similarities in terms of style and function, and maybe their relationship is something like this

Let’s start by saying how to define a variable, no matter what kind of programming depends on variables.

JavaScript TypeScript Java Python Go
Var variable name You can use js or var variable name: variable type Variable type Variable name; Do not discard the end of a semicolon Define a variable directly Var Variable name Variable type
var a var a:number int a; a var a int

In terms of form, ts and Go are the closest, and PY is the simplest. Go and Java need to specify data types

Comparison of data types

The base type

1. boolean

Booleans are the most common base type used to determine whether something is true or false. In JS, booleans are true and false, while in Python they are true and false

JavaScript TypeScript Java Python Go
var a = false var a:boolean = false boolean a = false; a = False var b bool = false
b := false

Go uses := to specify the type of a variable at the same time as the assignment, as does ts

 var a = 1
 a = "1" // Cannot assign type "string" to type "number". ts(2322)
Copy the code

2. number

Float32 float64 is used for mathematical operations. In go, the number type is usually int float32 float64, which corresponds to Java’s int float double. Js and PY use double floating-point types

JavaScript TypeScript Java Python Go
var a = 1 var a:number = 1 int a = 1; a = 1 var a int = 1
A: = 1.1

Data type comparison between GO and Java

Java Go
byte byte
short int8
int int
long int64
float float32
double float64
boolean bool
char rune

There is no char in go, but there is a similar rune in the official interpretation. Rune in Golang is an alias for int32, identical in every respect to int32, and is used to distinguish character values from integer values

s:="Hello, hello"
// Len is a function that calculates the length of the data. In py, len() is the js length property, and the Java length() method
len(s) // The length is 11
len([]rune(s))// The length is 7
Copy the code

3. string

Strings are also a common data type, but in Java, String is not a base type; its base data type is char

JavaScript TypeScript Java Python Go
var a = "str" var a:string = "str" String a = "str"; a = "str" var a string = "str"
var a = 'str' a := "str"
var a = `str` a := `str`

In js/ts/py, strings can be constructed using “” and “, and the effect is the same, whereas in Java “” represents strings and “” represents characters, and go uses “” to represent strings

Js /ts/go can also construct strings using backquotes, but go does not support escaping characters in this way

Java’s String, C++’s STD :: String, and Python3’s STR types are all just sequences of fixed-width characters. Go’s String is a utf-8 encoded sequence of variable-width characters, each of which is represented by one or more bytes.

A string is an immutable sequence of bytes. A string can contain arbitrary data, but is usually used to contain readable text. A string is a sequence of UTF-8 characters (1 byte if the character is on the ASCII code table, and 2-4 bytes if necessary for other characters).

That is, a Go language string is an arbitrary byte sequence of constants.

In fact, I think the most important sentence above is the one in parentheses. When a character is in the ASCII table, it takes up 1 byte, and the rest of the characters take up 2-4 bytes as needed. Java characters always take up 2 bytes, so the length of the go string is uncertain. The advantage of this is to save memory and hard disk, but also does not need to decode the whole UTF8, will be faster. I don’t know the actual situation

String A = "111AAA ahhhhh"
A.length() / / 9
Copy the code
A := "111AAA ahhhhh"
len(A) / / 15
len([]rune(A)) / / 9
Copy the code

annotation

The purpose of comments is to ignore something that doesn’t need to be run. Usually when we write code, we write a lot of code comments, and these comments are used to describe what the code does

1. Inline comments

In-line comments, which describe the behavior of the current block of code, can be useful when the logic is complex, and we recommend writing lots of code comments to simplify code writing.

TypeScript Java Python Go
// // # //
Var a = 1 // int a = 1 // int a = 1 // int a = 1 // int a = 1 //Copy the code

As you can see, the inline comments are the same except for Python

2. Cross-line comments

Cross-line comments are usually used to comment large chunks of code during debugging, but it is important to note that cross-line comments do not support nesting. Typically, cross-line comments are used outside a function to describe the main behavior of the function and how arguments are taken in and out.

TypeScript Java Python Go
/ * * / / * * / ' ' ' ' ' ' / * * /
"" "" ""
/* this is a javascript comment */ var a = 1 /* this is a Java comment */ int a = 1 "" this is a py comment" "a = 1 /* this is a go comment */ a := 1Copy the code

Python can use both single and double quotes for comments, essentially the same

File structure

1. The suffix

JypeScript TypeScript Java Python Go
.js .ts .java .py .go

2. Initialization differences of encoding files

2.1 JavaScript, TavaScript
// Initializations within the js file can be done without any code, since the file itself is executable
Copy the code
2.2 Java
package main; // Indicates the package location of the current file. If the file is in the root directory of the folder, package is main

public class ClassName { // ClassName corresponds to the file name classname.java
	public static void main(String args[]) {
    	// Java entry, other method names are not allowed}}Copy the code
2.3 the python
# py in-file initialization can be done without any code because the file itself is executable demo.py

# py runs in two ways,
If you run it directly, it will be executed from top to bottom, and if you run it from main, it will continue from here
# this code will not be triggered if it is called
# This is the entry to the file
if __name__ == 'main': 
	pass
Copy the code
2.4 the go
package main // Indicates the package location of the current file. If the file is in the root directory of the folder, package is main

func main(a) { // Default entry file, others not

}
Copy the code