Home > Common Problem > body text

Is golang a high-level language?

百草
Release: 2023-07-07 13:44:51
Original
754 people have browsed it

Golang is a high-level language. It is programming that is closer to natural language and mathematical formulas. It is basically separated from the hardware system of the machine and writes programs in a more understandable way. It is designed to solve practical problems in the development process of large systems. Designed to support concurrency, unified specifications, simplicity and elegance, and powerful performance, the main goal is to combine the development speed of dynamic languages ​​such as Python with the performance and security of compiled languages ​​such as C/C.

Is golang a high-level language?

The operating environment of this tutorial: Windows 10 system, GO version 1.20, Dell G3 computer.

go is a high-level language. Go language is a high-level programming language open sourced by Google in 2009. It is designed to solve practical problems in the development process of large systems. It supports concurrency, unified specifications, simplicity and elegance, and powerful performance; its main goal is "It combines the development speed of dynamic languages ​​​​such as Python with the performance and security of compiled languages ​​​​such as C/C."

Computer languages ​​are divided into high-level languages ​​and low-level languages. High-level language is mainly relative to assembly language. It is programming that is closer to natural language and mathematical formulas. It is basically separated from the hardware system of the machine and writes programs in a way that is easier for people to understand. The program written is called the source program.

High-level language does not refer to a specific language, but includes many programming languages, such as the popular go language, java, c, c, C#, pascal, python, lisp, prolog, FoxPro , Easy Language, Chinese version of C language, etc. The syntax and command format of these languages ​​are different.

Go language is a high-level programming language open sourced by Google in 2009. It is designed to solve practical problems in the development process of large-scale systems. It supports concurrency, unified specifications, simplicity and elegance, and powerful performance. It is used by many Go language evangelists hail it as "the C language in the cloud computing era." The main goal of the Go language is to "have both the development speed of dynamic languages ​​such as Python and the performance and security of compiled languages ​​such as C/C."

The Go language is sometimes described as a "C-like language", or "the C language of the 21st century". Go inherits similar expression syntax, control flow structure, basic data types, call parameter value transfer, pointers and many other ideas from C language. It also has the running efficiency of compiled machine code that C language has always valued and is consistent with existing Seamless adaptation to the operating system.

Pros and Cons of Go Programmer’s Voice: If the real world requires me to prototype, test, and deploy a production system in a few days and handle 5 times more requests per second, The CPU and memory overhead are still very small. I think only the Go language can do it.

Go language has the following advantages:

Separate binary release: Go project compilation will generate a static executable file. This file can be run without any other dependencies. This approach is particularly suitable for cloud-native container environments.

Cross-compilation: Compile binaries on any operating system that run on other platforms. For example, on a Mac system, binary files can be compiled that can run on Linux and Windows. Garbage collection: Go language supports garbage collection. In comparison, C, Rust, etc. require developers to control themselves. Execution performance: Go is very fast. Performance is close to C. Much higher than Java, Python, and Node. Development efficiency: Go language has both the running performance of static languages ​​and the development efficiency of dynamic languages.

Simplicity and efficiency: The design philosophy of the Go language includes simplicity and efficiency. A typical counterexample is the complex and bloated Java language. Concurrency: The language level supports concurrency, simplifies concurrent development through coroutines and channels, and improves concurrency performance.

Rich standard library: The Go standard library covers text, IO, network, encryption, Web services, remote RPC, template engine and other functions. C language can be called: C language functions can be called to further optimize performance and reuse the huge ecosystem of C language.

Fast compilation time: Go compiles very quickly. You can refer to two static blog generation systems, Hexo (developed by Node) and Hugo (developed by Go).

Engineering type: The purpose of Go language design is to become an engineering language to solve actual engineering problems. The Go language defines development specifications and provides a wealth of tools. Using Go language, you can write programs that are easy to read and understand, and easy to test, maintain and expand.

Go language has the following shortcomings:

Lack of heavyweight framework. Such as Ruby's Rails, Python's Django, and Java's Spring.

Error handling: No exception system. Go officials are fixing this issue.

Software package management: For a long time, Go has not officially had a package management system. Until recently, Go version 1.13 officially introduced Go Module as an official dependency management tool.

is not a standard object-oriented programming model: this is also an innovation of the Go language. If you are a solid OOP adherent, this may be a bit uncomfortable.

golang advanced syntax

rune

package main
import "fmt"
//rune相当于go的char  使用utf8编码,中文占3个字节,英文一个字节
func main() {
    s:= "ok我爱你"
    fmt.Println(len(s))    // 11
    fmt.Println(len([]rune(s)))  // 5
    fmt.Println(len([]byte(s)))  // 11
    // str是int32类型
    for i, str := range s {
        fmt.Printf("%d %c", i, str)
        fmt.Println()
    }
    // str是byte类型
    for i, str := range []byte(s) {
        fmt.Printf("%d %x", i, str)
        fmt.Println()
    }
    // str是rune类型
    for i, str := range []rune(s) {
        fmt.Printf("%d %c", i, str)
        fmt.Println()
        }
    }
Copy after login

slice slice

The bottom layer of slice is an array

slice is a view of the array

Slice can be extended backwards, but not Forward expansion

s[i] cannot exceed len(s), and backward expansion cannot exceed the underlying array cap(s)

Slice maintains 3 variables internally, and the ptr pointer points to The first element of the slice, len specifies the length of the slice, and cap specifies the capacity of the slice.

When the slice is appended, if the capacity is insufficient, it will be doubled.

有如下
arr := [...]{0, 1, 2, 3, 4, 5, 6, 7}
s1 := arr[2:6]
s2 := s1[3:5]
则
s1值为[2,3,4,5],  len(s1)=4, cap(s1)=6 
s2值为[5,6], len(s2)=2, cap(s2)=3
slice底层是数组
slice可以向后扩展,不可以向前扩展
s[i]不可以超过len(s), 向后扩展不可以超越底层数组cap(s)
Copy after login
接着上题
arr := [...]{0, 1, 2, 3, 4, 5, 6, 7}
s1 := arr[2:6]
s2 := s1[3:5]
s3 := append(s2, 10)
s4 := append(s3, 11)
s5 := append(s4, 12)
则
s1值为[2,3,4,5]
s2值为[5,6]
s3值为[5,6,10]
s4值为[5,6,10,11]
s5值为[5,6,10,11,12]
arr值为[0, 1, 2, 3, 4, 5, 6, 10] 
由于s4和时s5已经超过arr的cap,此时系统会生成一个新的数组,所以s4和s5是对新数组的view,即s4和s5 no longer view arr
Copy after login

If the cap is exceeded when adding elements, the system will reallocate a larger underlying array, and the original array will be copied. If no one uses the original array, it will be gc

due to the value The passed relationship must accept the return value of append

map

Go language, so types have default values

When the key of the map value does not exist, it will only be returned Default value, no error will be reported. To determine whether the key exists, use key, ok := m["key"]

map uses a hash table, and the keys of the map must be comparable

Except for slice, map, and function All built-in types can be used as key

The struce type does not contain the above fields, or it can be used as key

struct

Only using pointers can change the structure content

nil pointers can also call methods

How to expand system types or other people’s types: through structure inheritance, aliasing through types

package main
// 如何扩充系统类型或者别人的类型:通过结构体继承,通过类型起别名
type queue []int
func (q *queue) push(v int) {
    *q = append(*q, v)
    }
func (q *queue) pop() int {
     head := (*q)[0]*q = (*q)[1:]return head
     }
func (q *queue) isEmpty() bool {return len(*q) == 0
    }
func main() {
    }
Copy after login

Value receiver vs pointer receiver,

Value receivers are unique to the Go language

To change the content, you must use pointer receivers.

Consider using pointer receivers if the structure is too large.

Both value/pointer receivers can call value/pointer calls

package main
import "fmt"
type node struct {
value int
left, right *node
}
func newNode(value int) *node{
return &node{
value: value,
left:  nil,
right: nil,
}
}
func (n node) setVal(val int) {
n.value = val
}
func (n *node) setValue(vall int) {
n.value = vall
}
func (n node) print() {
fmt.Println(n.value)
}
func (n *node) travel() {
if n == nil {
return
}
fmt.Println(n.value)
n.left.travel()
n.right.travel()
}
func main() {
var root node
root = node{}
root.left = &node{value:5}
root.right = new(node)
root.left.right = &node{4, nil, nil}
root.right.left = newNode(7)
// 调用指针方法,相当于引用传递,可以改变外部的值
root.left.setValue(100)
fmt.Println(root.left.value)
// 值传递,调用值方法,方法内部不能改变外部值
root.left.setVal(99)
fmt.Println(root.left.value)
// 先序遍历
root.travel()
}
Copy after login

interface

Multi-purpose interface combination

defer

panic and Return does not affect the call of defer

The above is the detailed content of Is golang a high-level language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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