Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Can Go replace Linux script?

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

For the question of whether Go language can replace Linux script, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

People in Cloudflare like the Go language very much. We use it in many internal software projects and larger plumbing systems. But can we take it to the next level and use it as a scripting language for our favorite operating system, Linux? .

Short answer: why not? Go is relatively easy to learn, is not redundant and has a powerful ecological library that can be reused to prevent us from writing all the code from scratch. Some other potential advantages it may bring:

At first glance, the Go script seems easy to implement the shebang (#!...) of the Unix script. Support. Line shebang) is the first line of the script, with #! Start, and specify that the script interpreter is used to execute the script (for example, #! / bin/bash or #! / usr/bin/envpython), so the system knows exactly how to execute the script no matter what programming language is used. Go has used the gorun command to support interpreter-like calls to .go files, so just add the appropriate shebang line (#! / usr/bin/envgorun) to any .go file, set the executable state of the file, and have fun.

Is there any other way to support it

OK, it seems that shebang's approach does not provide us with a comprehensive solution. Is there any other way we can use it? Let's take a closer look at how the Linux kernel executes binaries. When you try to execute a binary / script (or any file with executable bit settings), your shell ends up using the Linuxexecve system call, passing it to the binary file system path, command line arguments, and currently defined environment variables. The kernel is then responsible for parsing the file correctly and creating a new process with the code in the file. Most of us know that Linux (and many other Unix-like operating systems) use the ELF binary format for its executables.

Core principles of Linux kernel

However, one of the core principles of Linux kernel development is to avoid the "vendor/formatlock-in" of any subsystem, which is part of the kernel. As a result, Linux implements a "pluggable" system that allows the kernel to support any binary format-all you need to do is write the right module that parses the format of your choice. If you take a closer look at the kernel source code, you will find that Linux supports more binary formats. For example, with the recent 4.14Linux kernel, we can see that it supports at least seven binary formats (intra-tree modules used in various binary formats usually have a binfmt_ prefix in their names). It is worth noting that the binfmt_script module is responsible for parsing the shebang lines mentioned above and executing scripts on the target system (not everyone knows that shebang support is actually implemented in the kernel itself rather than in shell or other daemons / processes).

But since we don't think shebang is the best choice for Go scripting, it seems that we need something else. Surprisingly, the Linux kernel already has a "other type" binary support module, which has the appropriate name binfmt_misc. This module allows administrators to dynamically add support for various executable formats directly from user space through a well-defined procfs interface, with detailed records.

This is the answer to the question about whether the Go language can replace the Linux script. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report