Examples
Func recv (clnsck net. Conn) {
buf: = Make ([]byte, 1024x768)
datalen, err: = Clnsck.read (BUF)
if err! = Nil {
FMT. PRINTLN (Err)
return
}
msg: = String (buf[:d Atalen])
//Displays the received message
FMT. PRINTLN (msg)
}
1.func recv (clnsck net. Conn)
Clnsck Net. Conn not very clear, net. Conn is a type that should be a listener type and defines a clnsck variable
2. BUF: = Make ([]byte, 1024)
But defined as a slice slice (defined by make, so each source array element is 0, altogether 1024),
The length capacity is 1024 and can read long enough (1024) character information.
Where byte refers to the 8 characters of Int8 2 (0~255 ASCLL2 code)
3. Datalen, err: = Clnsck.read (BUF)
Clnsck.read () where type clnsck defines the Read () method,
The parameter of its method is a slice slice, and the return value includes an int type representing the byte length (assigned to Datalen).
Another error type was assigned to err. Read to the end of the data stream (Err==io. EOF), break;
4.msg: = String (buf[:d Atalen])
Msg infers that it is a string type, buf[:d Atalen] means
From the 0 bits of the But slice until the first one of the datalen numbers,
Note that the numbers are shown here, one of the 0~255 numbers.
String () Converts all elements of the buf slice into character formatting.
Finally, the information obtained from CLNSCK does not include the last character (usually a carriage return), which can be known from the Datalen.