How to get the first line of a multi-line string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	multiLineString :=
		"First line\n" +
		"Second line\n" +
		"Third line\n" +
		"Fourth line"

	firstLine := strings.Split(multiLineString, "\n")[0]

	fmt.Println("The first line is: " + firstLine)
}


/*
run:

The first line is: First line

*/

 



answered Aug 13, 2024 by avibootz
...