القرّاء
القرّاء
تحدّد حزمة io واجهة io.Reader،
التي تمثّل طرف القراءة لتدفّق من البيانات.
تحتوي مكتبة غو القياسية على تنفيذات كثيرة لهذه الواجهة، منها الملفات، واتصالات الشبكة، وأدوات الضغط، وأدوات التشفير، وغيرها.
لواجهة io.Reader تابع Read:
func (T) Read(b []byte) (n int, err error)
Read يملأ شريحة البايتات المعطاة بالبيانات ويُرجع عدد البايتات
التي مُلئت وقيمة خطأ. ويُرجع خطأ io.EOF عندما يصل التدفّق
إلى نهايته.
تنشئ شيفرة المثال قارئًا من النوع
strings.Reader
وتستهلك خرجه بمقدار 8 بايتات في كل مرة.
النص الأصلي بالإنجليزية
Readers
The io package specifies the io.Reader interface,
which represents the read end of a stream of data.
The Go standard library contains many implementations of this interface, including files, network connections, compressors, ciphers, and others.
The io.Reader interface has a Read method:
func (T) Read(b []byte) (n int, err error)
Read populates the given byte slice with data and returns the number of bytes
populated and an error value. It returns an io.EOF error when the stream
ends.
The example code creates a
strings.Reader
and consumes its output 8 bytes at a time.
هذا الدرس للقراءة فقط؛ جرّب المثال في الدرس التالي.