Raspberry Pi with Go Lang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
This program achieves LED blink in Raspberry Pi with Go lang.//This is the implemented with Hard-coding and uses only main Function.package mainimport ("FMT" "OS" "Time") Func main () {//Initialize GPIO25 FMT . Println ("Initialize GPIO25") fd, ERR: = OS. OpenFile ("/sys/class/gpio/export", OS. O_wronly|os. O_sync, 0666) if err! = Nil {fmt. PRINTLN ("Open/sys/class/gpio/export fails") FMT. PRINTLN (ERR) return} FMT. Fprint (FD, "+") fd. Close ()//Check iinitialization result fmt. PRINTLN ("Check initialization Result") _, err = OS. Stat ("/sys/class/gpio/gpio25") if err! = Nil {fmt. Println ("Export GPIO25 fails") FMT. PRINTLN (Err)} else {fmt. Println ("Export GPIO25 succeeds")}//Set direction to-out FMT. Println ("Set direction of GPIO25 to Out") fd, err = os. OpenFile ("/sys/clasS/gpio/gpio25/direction ", Os. O_wronly|os. O_sync, 0666) if err! = Nil {fmt. PRINTLN ("open/sys/class/gpio/gpio25/direction fails") FMT. PRINTLN (Err)} FMT. Fprint (FD, "out") fd. Close ()//Start blink FMT. Println ("Start blink") for I: = 0; I < 20; i++ {FD, err: = OS. OpenFile ("/sys/class/gpio/gpio25/value", OS. O_wronly|os. O_sync, 0666) if err! = Nil {fmt. PRINTLN ("Open/sys/class/gpio/gpio25/value fails") FMT. PRINTLN (ERR)} if i%2 = = 1 {//Turn on LED FM T.fprint (FD, 1)} else {//Turn off LED fmt. Fprint (FD, 0)} FD. Close () time. Sleep (Time.millisecond)}//End FMT. PRINTLN ("Start finalizing") fd, err = os. OpenFile ("/sys/cLass/gpio/gpio25/value ", Os. O_wronly|os. O_sync, 0666) if err! = Nil {fmt. PRINTLN ("Open/sys/class/gpio/gpio25/value fails") FMT. PRINTLN (ERR)}//Turn off LED fmt. Println ("Turn off LED") fmt. Fprint (FD, 0) fd. Close () fd, err = os. OpenFile ("/sys/class/gpio/unexport", OS. O_wronly|os. O_sync, 0666) if err! = Nil {fmt. PRINTLN ("Open/sys/class/gpio/unexport fails") FMT. PRINTLN (ERR) return} FMT. Fprint (FD, "+") fd. Close () fmt. PRINTLN ("End finalizing")}

Http://shang.qq.com/wpa/qunwpa?idkey=912ee9f1c4d48fd64877a62ce321ca041ff6780dee63a5ccaa07241719ea5be9 Multi-language/ High Concurrency Research group

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.