Home »
Golang »
Golang Reference
Golang os.SEEK_SET Constant with Examples
Golang | os.SEEK_SET Constant: Here, we are going to learn about the SEEK_SET constant of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 26, 2021
os.SEEK_SET
In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The SEEK_SET constant is used to seek relative to the origin of the file i.e., it specifies the starting byte of the file pointer, it is used with the Seek() function to seek the file pointer at the given position from the starting position of the file pointer.
The value of SEEK_SET constant 0.
Syntax
SEEK_SET int
Implementation in the package source code:
SEEK_SET int = 0
Parameters
Return Value
The return type of the os.SEEK_SET constant is an int, it returns 0 i.e., the value of SEEK_SET constant is 0.
Example 1
// Golang program to demonstrate the
// example of SEEK_SET constant
package main
import (
"fmt"
"os"
)
func main() {
// Printing the type and value
fmt.Printf("Type of os.SEEK_SET: %T\n",
os.SEEK_SET)
fmt.Printf("Value of os.SEEK_SET: %d\n",
os.SEEK_SET)
}
Output:
Type of os.SEEK_SET: int
Value of os.SEEK_SET: 0
Example 2
// Golang program to demonstrate the
// example of SEEK_SET constant
package main
import (
"fmt"
"io"
"os"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Write the data to the file
f, err := os.Create("file1.txt")
check(err)
defer f.Close()
data := []byte("Hello, world!")
n2, err := f.Write(data)
check(err)
fmt.Printf("Wrote %d bytes\n", n2)
defer f.Close()
// Read the data from the file
f, err = os.Open("file1.txt")
check(err)
defer f.Close()
filedata := make([]byte, 15)
n1, err := f.Read(filedata)
check(err)
fmt.Printf("File data: %d bytes: %s\n", n1, string(filedata[:n1]))
// Seeking to start i.e., at 0th position
// and read the content
f.Seek(0, os.SEEK_SET)
n1, err = io.ReadAtLeast(f, filedata, 5)
check(err)
fmt.Printf("File data: %d bytes: %s\n", n1, string(filedata[:n1]))
// Seeking to 5th position
// and read the content
f.Seek(5, os.SEEK_SET)
n1, err = io.ReadAtLeast(f, filedata, 5)
check(err)
fmt.Printf("File data: %d bytes: %s\n", n1, string(filedata[:n1]))
}
Output:
Wrote 13 bytes
File data: 13 bytes: Hello, world!
File data: 13 bytes: Hello, world!
File data: 8 bytes: , world!
Golang os Package »