Go language Web programming, beginner bit record 1

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

Almost all of the code comes from:

http://jan.newmarch.name/go/

Thanks to the author

/* IP */package mainimport ("FMT" "NET" "OS") Func main () {if Len (OS. Args)! = 2 {fmt. fprintf (OS. Stderr, "Usage:%s ip-addr\n", OS. ARGS[0]) os. Exit (1)}name: = OS. ARGS[1]ADDR: = Net. PARSEIP (name) if addr = = Nil {fmt. Println ("Invalid address")} else {fmt. Println ("The address is", addr. String ())}os. Exit (0)}

The net package is used mainly.

Os. Args is a command-line argument

It's a slice slice of a string.

Net. Parseip () as implies meaning is converted to an IP address is actually the "192.168.1.1" such a string into the form of IP in the Go language, the Go language IP is []byte type, this thing a slice slice. This app may not be much use, but the following version may be useful:

/* IP */package mainimport ("FMT" "NET" "OS") Func main () {if Len (OS. Args)! = 2 {fmt. fprintf (OS. Stderr, "Usage:%s ip-addr\n", OS. ARGS[0]) os. Exit (1)}name: = OS. ARGS[1]//ADDR: = Net. PARSEIP (name) addr, _:=net. Lookuphost (name) if addr[0] = = "" {fmt. Println ("Invalid address")} else {fmt. Println ("The address is" +addr[0])}os. Exit (0)}

After compiling successfully: run./IP www.baidu.com

Getting results: The address is 123.125.114.144

Net. Lookuphost () View the IP address of the domain name, the official explanation is:Lookuphost looks up the given host using the local resolver. It returns an array of the that host ' s addresses.

Hey


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.