io.WriteString() Function in Golang with Examples Last Updated : 05 May, 2020 Comments Improve Suggest changes Like Article Like Report In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The WriteString() function in Go language is used to write the contents of the stated string "s" to the writer "w", which takes a slice of bytes. And if "w" is implemented by StringWriter then its WriteString method is called immediately. Else, w.Write is invoked strictly once. Moreover, this function is defined under the io package. Here, you need to import the "io" package in order to use these functions. Syntax: func WriteString(w Writer, s string) (n int, err error) Here, "w" is the writer, and "s" is the string that is written to the writer. Return value: It returns the total number of bytes of the content of type int and also returns an error if any. Below examples illustrates the use of above method: Example 1: C // Golang program to illustrate the usage of // io.WriteString() function // Including main package package main // Importing fmt, io, and os import ( "fmt" "io" "os" ) // Calling main func main() { // Defining w using Stdout w := os.Stdout // Calling WriteString method with its parameters n, err := io.WriteString(w, "GfG\n") // If error is not nil then panics if err != nil { panic(err) } // Prints output fmt.Printf("n: %d\n", n) } Output: GfG n: 4 Example 2: C // Golang program to illustrate the usage of // io.WriteString() function // Including main package package main // Importing fmt, io, and os import ( "fmt" "io" "os" ) // Calling main func main() { // Defining w using Stdout w := os.Stdout // Calling WriteString method with its parameters n, err := io.WriteString(w, "GeeksforGeeks\nis\na\nCS-Portal.\n") // If error is not nil then panics if err != nil { panic(err) } // Prints output fmt.Printf("n: %d\n", n) } Output: GeeksforGeeks is a CS-Portal. n: 30 Here, in the above example, "Stdout" is used in order to create a default file descriptor where the stated content is written. Comment More infoAdvertise with us Next Article io.WriteString() Function in Golang with Examples nidhi1352singh Follow Improve Article Tags : Go Language Golang-io Similar Reads io.MultiWriter() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The MultiWriter() function in Go language is used to create a writer that copies its writes to each and every given writers, wh 3 min read io.PipeWriter.Write() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The PipeWriter.Write() function in Go language is used to implement the standard interface of the Write. It writes information 3 min read io.Pipe() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The Pipe() function in Go language is used to create a concurrent in-memory pipe and can be applied in order to link the code t 3 min read io.Copy() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The Copy() function in Go language is used to copy from the stated src i.e, source to the dst i.e, destination till either the 3 min read io.CopyN() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The CopyN() function in Go language is used to copy "n" bytes from source to the destination. And if dst is implemented by the 3 min read strings.Join() Function in Golang With Examples strings.Join() Function in Golang concatenates all the elements present in the slice of string into a single string. This function is available in the string package. Syntax: func Join(s []string, sep string) string Here, s is the string from which we can concatenate elements and sep is the separato 1 min read io.PipeWriter.Close() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The PipeWriter.Close() function in Go language is used to close the writer. However, successive reads from the PipeReader i.e, 3 min read io.LimitReader() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The LimitReader() function in Go language is used to return a "Reader" that reads from the stated "r" but it pauses if the EOF 2 min read io.ReadAtLeast() Function in Golang with Examples In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The ReadAtLeast() function in Go language is used to read from the stated reader "r" into the stated buffer "buf" until it has 3 min read strings.SplitN() Function in Golang with Examples strings.SplitN() Function() is a string manipulation function in Go language. It is used to split a given string into substrings separated by a separator. This function returns the slices of all substrings between those separators. Syntax: func SplitN(s, sep string, n int) []string Here, s is the st 3 min read Like