Devdit
 

cannot use n (type []string) as type []int in argument to sort.Ints Go คือ วิธีแก้ไข

1.1K

รันโค้ดเรียงลำดับตัวอักษร ภาษา Go (Golang) แต่ไม่ผ่านขึ้นข้อความว่า cannot use n (type []string) as type []int in argument to sort.Ints แบบนี้ต้องแก้ไขอย่างไร

func main() {
	s := []string{"apple", "banana", "orange"}
	sort.Ints( s )
	fmt.Println( s )
}

 

วิธีแก้ไข

เปลี่ยน sort.Ints เป็น sort.Strings เนื่องจากตัวแปร array เป็นชนิด string ซึ่งเวลาจะเรียงตัวอักษร/ข้อความ ต้องใช้คำสั่ง sort.Strings จาก package sort

func main() {
	s := []string{"apple", "banana", "orange"}
	sort.Strings( s )
	fmt.Println( s )
}
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ