Some basic operations commonly used by Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The following are some common operations golang, get environment variables, operation files and so on;

The package main import ("FMT" "OS" "Path/filepath" "Io/ioutil") func main () {//connects the current path and the given path to an absolute path abspath,_: = FilePath. Abs ("Test") fmt. PRINTLN (Abspath)//Returns the last path (usually a directory name or file name) BasePath: = FilePath. Base (Abspath) fmt. PRINTLN (BasePath)//Returns a path other than the last directory (that is, the path before the last directory) Dirpath: = FilePath. Dir (Abspath) fmt. PRINTLN (Dirpath)//Returns the file suffix name (for example,. txt) in the path with the file name: = "C:\\1.txt" FileName: = filepath. EXT (file) fmt. PRINTLN (fileName)//replace '/' in the path with ' \ ', multiple '/' is replaced by multiple ' \ \ ' slash: = FilePath. Fromslash ("D://goproject//src//example//test") fmt. Println (slash)//The connection path becomes a full path joinpath: = FilePath. Join ("C:\\go\\test", "example") fmt. PRINTLN (Joinpath)//Output Execution program name (Test_filepath.exe) fmt. PRINTLN (filepath. Base (OS. ARGS[0])//Output Execution program information FMT. Println (OS. Stat (OS. ARGS[0])//Get subdirectories under a directory List,err: = Ioutil. ReadDir ("D:\\goproject\\src\\example") if err! = Nil {fmt. PRINTLN (Err)}for _, Item: = Range List {if item. Isdir () {fmt. PRINTLN (item. Name ())}}//determines whether the running user of the current process is rootfmt. Println (OS. Geteuid ())//Get environment variable FMT. Println (OS. Getenv ("Java_home"))}
Summary: Golang Basic operation is more, such as file operation, very detailed, please refer to the Golang API for detailed content. api:https://golang.org/doc/

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.