Explore essential packages, functions, and usage patterns from the Go standard library. This quiz helps reinforce core Go concepts, covering input/output, string processing, error management, and data structures – key for programmers aiming to master foundational Go skills.
Which package provides the Contains function to check if a substring is present within a string?
Explanation: The strings package offers the Contains function, which returns true if the substring exists within the main string. The strconv package is intended for type conversions between strings and other basic data types. The io package is used for input and output operations, not string manipulation. The fmt package is mainly for formatted I/O, not for searching within strings.
Which function from the bufio package reads a line of text from standard input in Go?
Explanation: ReadString in the bufio package can be used with an appropriate delimiter, such as a newline, to read a line of text. ScanLine is not an actual function in bufio—Scan is used within bufio.Scanner. ReadAll is found in io/ioutil and reads entire data streams, not a single line. InputLine does not exist in the standard library.
Which function would you use to print formatted text with a new line at the end to the standard output?
Explanation: fmt.Println displays arguments followed by a newline, making it ideal for printing lines of text. output.Print and os.Output are not real functions in Go’s standard library. fmt.Write does not exist; the equivalent function for writing is fmt.Fprintf or similar.
How do you create a new error with a custom string message in Go using the standard library?
Explanation: The errors.New function creates a basic error type with a given message. fmt.NewError and error.Make are not functions provided in Go. errors.Create also does not exist; you should use errors.New for this purpose.
Which package is used to encode and decode JSON data in Go?
Explanation: The encoding/json package provides marshaling and unmarshaling functions for JSON. net/json and jsonlib are not standard packages. serialization is a general term and not a package in Go.
To format dates and times in Go, which package provides the Format method?
Explanation: The time package offers the Format method for formatting time.Time values according to layout patterns. datetime, clock, and calendar are not recognized Go standard library packages. Only time contains the relevant formatting functionality.
Which standard package is commonly used to generate random integers in Go?
Explanation: math/rand is the standard package for generating pseudo-random numbers, including whole numbers with its Intn and similar functions. randomizer and integers are not standard packages, while random is not available under that name in Go.
Which function from the os or io/ioutil package reads an entire file into memory as a byte slice?
Explanation: ioutil.ReadFile reads an entire file and returns its contents as a slice of bytes. os.ReadData, file.ReadBytes, and os.GetFile are not standard functions provided for file reading in Go. ioutil.ReadFile is commonly used for this task.
Which function sorts a slice of integers in ascending order in the standard library?
Explanation: The sort.Ints function is designed to sort slices of integers in ascending order. slice.Sort and ints.Sort are not valid functions in Go. array.Sort does not exist either; the correct approach uses sort.Ints.
Which function can you use to obtain the current working directory of your Go program?
Explanation: os.Getwd returns a string with the current working directory and an error value if something goes wrong. os.WorkDir, os.CurrentDir, and os.GetDir are not actual functions in the Go standard library. Only os.Getwd retrieves the working directory path.