This is a creation in Article, where the information may have evolved or changed. The I/O core of Go is interface Io.reader and Io.writer
Example: (read from file) unbuffered package Mainimport "OS" func Main () {buf: = make ([]byte, 1024x768) F, _: = OS. Open ("/etc/passwd")//opening file, OS. Open returns a *os that implements the Io.reader and Io.writer. File; defer f.close ()//Make sure F is off; for {n, _: = F.read (BUF)//Read 1024 bytes at a time if n = = 0 {//reach end of file break; } OS. Stdout.write (Buf[:n])//writes the content to the OS. Stdout}} Output: Content in the/etc/passwd file
If you want to use buffer Io, there is a BUFIO package
Example: (read from file) there is a buffer package mainimport ("OS" "Bufio") func main () {buf: = make ([]byte, 1024x768) F, _: = OS. Open ("/etc/passwd") defer F.close () r: = Bufio. Newreader (f)//conversion F is a buffered reader. Newreader requires a io.reader w: = Bufio. Newwriter (OS. Stdout) defer W.flush () for {n, _: = R.read (BUF)//read from reader, write to writer, and output files to the screen if n = = 0 {break; } w.write (Buf[0:n])}} Output Result: Contents of/etc/passwd file
command-line argumentsThe arguments from the command line are Sliceos through the string in the program. Args gets, imports the package OS. The flag packet has a sophisticated interface, which also provides a way to parse the identity.
Execute CommandThe OS/EXEC package has functions to execute external commands, which is also the main way to execute commands in Go
Example: Package Mainimport "FMT" import "Os/exec" Func Main () {cmd: = Exec.command ("/bin/ls", "-L") buf,_: = cmd. Output () For _, Val: = Range buf {fmt. Printf ("%c", Val)}} Output Result: Total 4-rw-r--r--1 root root 173 Jan 15:22 demo.go
NetworkAll network-related types and functions can be found in the net package. The most important of these functions is dial. When dial to a remote system, this function returns the Conn interface type, which can be used to send or receive information. The function dial concise abstraction of the network layer and the Transport layer. So IPv4 or ipv6,tcp or UDP can share an interface.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service