This is a creation in Article, where the information may have evolved or changed. In the go language, when a multidimensional array is passed directly as a function argument, it can be very restrictive, for example, except that the other dimensions of the first-dimension array need to be explicitly given; You can use multidimensional slices to pass as parameters:
Type S1 []int
Type S2 []s1
Sword means offer a written question 3:
In a two-dimensional array, each row is sorted from left to right in ascending order, and each column is sorted from top to bottom. Complete a function, enter a two-dimensional array and the number to find, and determine whether the number exists in a two-dimensional array.
Code:
Package Mainimport ("Errors" "FMT" "OS") type S1 []inttype s2 []s1var searcherr error = errors. New ("Search Error") Func main () {A: = S2{s1{1, 2, 3, 4, 5, 6},s1{7, 8, 9, ten, one, 12},s1{13, +,,, 18},s1{20, 21, 25}}result, Err: = Search (a[:][:], 4, 6, +) if err! = Nil {fmt. PRINTLN (ERR) OS. Exit (1)}if result = = true {fmt. Println ("Key is searched")}}func search (a s2, row, col, Key int) (flag bool, err error) {if row < 0 | | Col < 0 {Retu RN false, Searcherr}flag = Falserownum: = 0colNum: = Col-1for rowNum < row && Colnum >= 0 {if A[rownum][co Lnum] = = key {flag = Truebreak} else if a[rownum][colnum] > key {colnum--} else {Rownum++}}return}