Greetings,I choose to learn and write a program in Go for a Programming class. I am having a bit of trouble sorting my strings by alphabetical order. Here is my code:func main() {fmt.Println("Enter 10 Names")var names [10]stringfor i := 0; i < 10; i++ {in := bufio.NewReader(os.Stdin)line, _ := in.ReadString('\n')names[i] = line}fmt.Println("Print the names in the order they were entered.")for j := 0; j < 10; j++ {fmt.Println(names[j])}fmt.Println("Sort the names in alphabetical order, and print the sorted list.")sort.Strings(names)fmt.Println(names)I get the following error from my compiler: cannot use names (type [10]String) as type []string in argument to sort.Strings.Can someone point me in the right direction?Thanks in advance!
var names = make([]string, 10) // a slice of strings with an initial length and capacity of 10
I have adjusted my code to reject the following:import ("bufio""fmt""os""sort""strings")func SortString(w string) string {s := strings.Split(w, "")sort.Strings(s)return strings.Join(s, "")
}func main() {fmt.Println("Enter 10 Names")
// a slice of strings with an initial length and capacity of 10var names = make([]string, 10)
for i := 0; i < 10; i++ {in := bufio.NewReader(os.Stdin)line, _ := in.ReadString('\n')names[i] = line}
fmt.Println("\n")
fmt.Println("Print the names in the order they were entered.")
fmt.Println("\n")
for j := 0; j < 10; j++ {fmt.Println(names[j])}fmt.Println("Sort the names in alphabetical order, and print the sorted list.")
--fmt.Println("\n")x := SortString(names)fmt.Println(x)fmt.Println("\n")fmt.Println("Reverse the order of your names, and print them in that order.")fmt.Println("\n")for l := 10; l > 0; l-- {fmt.Println(names[l-1])}}*****When trying to run the program, I receive the following error:cannot use names (type []string) as type string in argument to SortString.I have attempted to figure out a solution, but I am lost........Any Suggestions?
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.