Constructing an absolute path

Report a typo

Kendrick wants to create a Go program that takes as input a number of whitespace-separated string Linux/macOS path elements, joins the path elements, and then checks if the joined path is absolute or not.

Kendrick has already written the code to read the whitespace-separated string path elements from the stdin! Your task is to help Kendrick write the required additional code to join the path elements and check whether they're absolute.

This problem uses the bufio package to scan the whitespace-separated path elements and the strings package to split the input into the pathElements slice. To solve this task, you don't need to know how the bufio package works. The only objective is to join the path elements and check if they are absolute or not.

Sample Input 1:

. /java /fonts

Sample Output 1:

java/fonts is not an absolute path!
Write a program in Go
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)

func main() {
// DO NOT delete this code block! it reads whitespace-separated string path elements from the stdin!
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
pathElements := strings.Split(scanner.Text(), " ")

// join the unpacked slice 'pathElements...' into a single path below:
// remember that in Go the '...' syntax after the slice name unpacks the slice!
path := ?.?(pathElements...)

// check if 'path' is absolute and print the result below:
if ?.?(?) {
fmt.Println(?, "is an absolute path!")
} else {
fmt.Println(?, "is not an absolute path!")
}
}
___

Create a free account to access the full topic