當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang io.SectionReader.Size()用法及代碼示例

在Go語言中,io軟件包為I /O原語提供基本接口。它的主要工作是封裝此類原始之王的正在進行的實現。 Go語言中的SectionReader.Size()函數用於查找NewSectionReader()方法返回的節的大小(以字節為單位)。而且,此函數在io包下定義。在這裏,您需要導入“io”包才能使用這些函數。

用法:

func (s *SectionReader) Size() int64

在這裏,“s”是指向由NewSectionReader方法返回的SectionReader的指針。

返回值:它返回以字節為單位的int64類型的節的大小。

範例1:



// Golang program to illustrate the usage of 
// io.SectionReader.Size() function 
  
// Including the main package 
package main 
  
// Importing fmt, io, and strings 
import ( 
    "fmt"
    "io"
    "strings"
) 
  
// Calling main 
func main() { 
  
    // Defining reader using NewReader method 
    reader:= strings.NewReader("Geeks") 
  
    // Calling NewSectionReader method with its parameters 
    r:= io.NewSectionReader(reader, 1, 4) 
  
    // Calling Size method 
    size:= r.Size() 
  
    // Prints output 
    fmt.Printf("The size of the section in bytes is:%v\n", size) 
}

輸出:

The size of the section in bytes is:4

範例2:

// Golang program to illustrate the usage of 
// io.SectionReader.Size() function 
  
// Including main package 
package main 
  
// Importing fmt, io, and strings 
import ( 
    "fmt"
    "io"
    "strings"
) 
  
// Calling main 
func main() { 
  
    // Defining reader using NewReader method 
    reader:= strings.NewReader("GeeksforGeeks\nis\na\nCS-Portal.\n") 
  
    // Calling NewSectionReader method with its parameters 
    r:= io.NewSectionReader(reader, 5, 23) 
  
    // Calling Size method 
    size:= r.Size() 
  
    // Prints output 
    fmt.Printf("The size of the section in bytes is:%v\n", size) 
}

輸出:

The size of the section in bytes is:23



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 io.SectionReader.Size() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。