Home »
Golang »
Golang Reference
Golang os.SEEK_END Constant with Examples
Golang | os.SEEK_END Constant: Here, we are going to learn about the SEEK_END constant of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 26, 2021
os.SEEK_END
In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The SEEK_END constant is used to seek relative to the end i.e., it specifies the end position of the file pointer, it is used with the Seek() function to seek the file pointer at the given position from the end position of the file pointer.
The value of SEEK_END constant 2.
Syntax
SEEK_END int
Implementation in the package source code:
SEEK_END int = 2
Parameters
Return Value
The return type of the os.SEEK_END constant is an int, it returns 2 i.e., the value of SEEK_END constant is 2.
Example 1
// Golang program to demonstrate the
// example of SEEK_END constant
package main
import (
"fmt"
"os"
)
func main() {
// Printing the type and value
fmt.Printf("Type of os.SEEK_END: %T\n",
os.SEEK_END)
fmt.Printf("Value of os.SEEK_END: %d\n",
os.SEEK_END)
}
Output:
Type of os.SEEK_END: int
Value of os.SEEK_END: 2
Example 2
// Golang program to demonstrate the
// example of SEEK_END 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
// from the end and read the content
f.Seek(-13, os.SEEK_END)
n1, err = io.ReadAtLeast(f, filedata, 5)
check(err)
fmt.Printf("File data: %d bytes: %s\n", n1, string(filedata[:n1]))
// Seeking to 6th position from the end
// and read the content
f.Seek(-6, os.SEEK_END)
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: 6 bytes: world!
Golang os Package »