Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Go Standard Library Cookbook
  • Table Of Contents Toc
Go Standard Library Cookbook

Go Standard Library Cookbook

By : Radomír Sohlich
1 (1)
close
close
Go Standard Library Cookbook

Go Standard Library Cookbook

1 (1)
By: Radomír Sohlich

Overview of this book

Google's Golang will be the next talk of the town, with amazing features and a powerful library. This book will gear you up for using golang by taking you through recipes that will teach you how to leverage the standard library to implement a particular solution. This will enable Go developers to take advantage of using a rock-solid standard library instead of third-party frameworks. The book begins by exploring the functionalities available for interaction between the environment and the operating system. We will explore common string operations, date/time manipulations, and numerical problems. We'll then move on to working with the database, accessing the filesystem, and performing I/O operations. From a networking perspective, we will touch on client and server-side solutions. The basics of concurrency are also covered, before we wrap up with a few tips and tricks. By the end of the book, you will have a good overview of the features of the Golang standard library and what you can achieve with them. Ultimately, you will be proficient in implementing solutions with powerful standard libraries.
Table of Contents (13 chapters)
close
close

Retrieving child process information

The recipe Calling an external process describes how to call the child process, synchronously and asynchronously. Naturally, to handle the process behavior you need to find out more about the process. This recipe shows how to obtain the PID and elementary information about the child process after it terminates.

The information about the running process could be obtained only via the syscall package and it is highly platform-dependent.

Getting ready

Test if the sleep (timeout for Windows) command exists in the Terminal.

How to do it...

  1. Open the console and create the folder chapter01/recipe09.
  2. Navigate to the directory.
  3. Create the main_running.go file with the following content:
        package main

import (
"fmt"
"os/exec"
"runtime"
)

func main() {

var cmd string
if runtime.GOOS == "windows" {
cmd = "timeout"
} else {
cmd = "sleep"
}
proc := exec.Command(cmd, "1")
proc.Start()

// No process state is returned
// till the process finish.
fmt.Printf("Process state for running process: %v\n",
proc.ProcessState)

// The PID could be obtain
// event for the running process
fmt.Printf("PID of running process: %d\n\n",
proc.Process.Pid)
}
  1. Run the code by executing go run main_running.go.
  2. See the output in the Terminal:
  1. Create the main.go file with the following content:
        func main() {

var cmd string
if runtime.GOOS == "windows" {
cmd = "timeout"
} else {
cmd = "sleep"
}

proc := exec.Command(cmd, "1")
proc.Start()

// Wait function will
// wait till the process ends.
proc.Wait()

// After the process terminates
// the *os.ProcessState contains
// simple information
// about the process run
fmt.Printf("PID: %d\n", proc.ProcessState.Pid())
fmt.Printf("Process took: %dms\n",
proc.ProcessState.SystemTime()/time.Microsecond)
fmt.Printf("Exited sucessfuly : %t\n",
proc.ProcessState.Success())
}
  1. Run the code by executing go run main.go.
  2. See the output in the Terminal:

How it works...

The os/exec standard library provides the way to execute the process. Using Command, the Cmd structure is returned. The Cmd provides the access to process the representation. When the process is running, you can only find out the PID.

There is only a little information that you can retrieve about the process. But by retrieving the PID of the process, you are able to call the utilities from the OS to get more information.

Remember that it is possible to obtain the PID of the child process, even if it is running. On the other hand, the ProcessState structure of the os package is available, only after the process terminates.

See also

There are Reading/writing from the child process and Calling an external process recipes in this chapter that are related to process handling.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Go Standard Library Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon