FAQ
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]string
     for 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!

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Matt Silverlock at Dec 4, 2015 at 7:14 am
    Read this: https://blog.golang.org/slices :)


    On Friday, December 4, 2015 at 2:21:36 PM UTC+8, culver...@gmail.com wrote:

    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]string
    for 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!
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Dec 4, 2015 at 7:17 am
    Names is an array, which is a distinct type. Almost everything in Go takes a slice, and for your example you probably want to declare names to be to.

    var names = make([]string, 10) // a slice of strings with an initial length and capacity of 10

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Culver Austin at Dec 5, 2015 at 1:16 am
    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 10
    var 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Dec 5, 2015 at 4:57 am
    Think about the type of your names variable, and the type of the
    sort.Strings function. Also your SortString function probably doesn't do
    what you think it does.
    On Fri, Dec 4, 2015 at 5:16 PM wrote:

    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 10
    var 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Austin Culver at Dec 5, 2015 at 6:37 am
    What function would I use to sort my string array?
    On Dec 4, 2015, at 11:57 PM, Matt Harden wrote:

    Think about the type of your names variable, and the type of the sort.Strings function. Also your SortString function probably doesn't do what you think it does.
    On Fri, Dec 4, 2015 at 5:16 PM wrote:
    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 10
    var 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Dec 5, 2015 at 7:48 am
    sort.Strings, and not SortString.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 4, '15 at 6:21a
activeDec 5, '15 at 7:48a
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase