Programming in Go
Regular Expressions
The fields can be processed with the fmt.Scanf
function, which works in the same way as the corresponding function in C: Format strings specify the format of the read row and the data type. Alternatively – but possibly a bit more slowly – you can do the same thing with regular expressions. To this end, Go offers the regexp
package [3], which implements the RE2 regular expression syntax [4].
The functions provided by the module are a bit confusing but do follow a system. First, you'll see two variants of each function: one that processes a byte array and one with the word String
in the name, which deals with strings. Then, you'll see functions that return the string found once only, or more than once (All). If you use multiple search strings in a regular expression and assign variables to the strings you find, you will want to use a function with Submatch
in its name.
In this example, you need the FindStringSubmatch
function because the regex pattern is designed so that it can match only once anyway, while storing all the finds in one array. The byte array from ReadFile
is converted to a string by typecasting. If you were to use the byte array variants of the regular expression functions, you would then continue to work with byte arrays or convert them.
Several functions are available to compile the search pattern: Compile
, CompilePOSIX
, MustCompile
, and MustCompilePOSIX
. Why this is so is not completely apparent, because the same thing could have been implemented with a single function that processes the corresponding parameters. To prevent the regular expression being constantly recompiled, you can define it as a top-level variable. The corresponding code is shown in Listing 1.
Listing 1
Regular Expressions
Line 14 of the code creates a new ProcData
object and assigns the first two submatches to its attributes pid
and name
. In line 9, the regular expression is contained in backticks (`
) because a backslash (\
), is regarded as an escape character in strings in single or double quotes. You would otherwise need to type another backslash in front of each one you really needed. In the backtick environment, you can save yourself the trouble.
The procedure for the /proc/PID/status
file, which you also need to read, is similar because the user ID of the process does not appear in the stat
file. However, the status file is a little more difficult to parse because it contains many individual lines. You could process the file line by line or use a regular expression in multiline mode enabled by the m
switch at the start of the regex. This alone, however, was not quite enough; I also needed the s
switch, which ensures that the dot meta-variable (.
) in the regular expression also includes the newline character (\n
). The regex for reading the user and group ID from the status file, thus looks like this:
(?sm)^Uid:\t(\d+).*^Gid:\t(\d+)
The UID is now assigned to a field in the ProcData
object.
The remaining task is to determine the UID for the user name – for example, by parsing the passwd
file. However, Go can save you this work because it has the user.LookupId
function for precisely this task. As a result, it returns a user
object containing the user name, among other things. All told, the procedure looks like this:
user, _ := user.LookupId(procData.uid) procData.user = user.Username
One option of the lap
tool is to determine whether to output the name or UID. For easy implementation of command-line options, Go includes the flag
package. Like everything else in Go, flags are typed and are available in various flavors (e.g., integer, string, and Boolean). A Boolean switch is defined by the line:
var realname = flag.Bool("r", false, "show real user name") flag.Parse()
The first parameter specifies the name of the switch, followed by the default configuration and the explanatory text. Later in the program, you can query the variable using if(realname)
.
When called with -r
, lap
then returns the user name; otherwise, it returns the UID (Figure 3). The main features of the program are shown in Listing 2; the complete listing is available online [5].
Listing 2
lap.go
Missing Bits
With relatively little effort, a small Go tool has been developed to identify the processes on a Linux system and display them at the command line. However, you could improve this by adding a cache for username lookup, for example. The run-time information of the processes (i.e., the start times, etc.) is also missing and could be added to the tool.
Infos
- "Go Programming Language" by Oliver Frommel, ADMIN, issue 11, 2013, pg. 90
- The /proc filesystem: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
- Go regexp package: http://golang.org/pkg/regexp/
- RE2 syntax: https://code.google.com/p/re2/wiki/Syntax
- Listings: ftp://ftp.linux-magazin.com/pub/listings/magazine/162
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.