ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Easy Programing

    ลำดับตอนที่ #7 : Go I - Go Fundamentals

    • เนื้อหาตอนนี้เปิดให้อ่าน
    • 23
      0
      2 ม.ค. 62

    Format

    Formatting Code

    It's important to write code that is easy to read and update for other people. Compare the two examples below. The code on the right has uniform spacing and indentation that makes it easier to read. Since Go has rules for format, we have prepared a button to automatically format the code in the exercise pages. Be sure to check it out!



    Basics of Go

    What are Strings?

    Now you know how to print words in the console! The "Hello, Go" that you just printed is called a string in the programming world. Strings must be put in double quotes. There will be an error if they are not used.


    Output Statements

    The println that you learned is one of many commands that are built-in with Go. This command means "print the content inside of println() in the console". In programming, you will put together various commands for the computer to complete. Also, take note that the l in println is a lower case L (not a capital i or number 1).


    Program Structure

    In the main.go file below, there is some other code in addition to the print statements. Files written in Go code like main.go are made up of parts like package definitions and function definitions. For now, just remember the code written inside the {} of the main function in package.main will run in the order it's written.


    How to Write Comments

    You can put comments in code, too. Since they won't affect the result, they are useful for writing notes about the program. There are two ways to write comments, block comments and line comments. In block comments, anything between /* and */ will become a block comment and using // at the start of a line with make a single-line comment.



    Integers

    Using Integers

    Integers are another fundamental part of Go. Unlike strings, integers do not have to be put in double quotes. You can also use them to do calculations such as addition and subtraction as shown below. You can use + for addition and - for subtraction.


    The Difference Between Integers and Strings

    In programming, integers and strings are completely different. In the example below, "1 + 3" seems to be printed for both. But, while the first println will print 4, the second one will print 1 + 3 because the value is in double quotes. Even when values are printed as strings, the double quotes will not be printed in the console.



    Calculations

    Doing Calculations with Go

    Other calculations besides addition and subtraction can be done with Go. For example, * is used for multiplication and / for division. Also, % can be used to calculate the remainder after division. These symbols are called "operators". Operators can be different from language to language.



    Combining Strings

    In Go, calculations can be done with things other than integers. The operator + will add things when integers are used, but for strings, it will combine strings to make them one string. In the example below, the strings "Hi" and "Gophers" will be combined using the + operator and become one string; "Hi,Gophers".


    The Difference Between Strings and Integers

    Calculations using the + operator have a different result when used with integers than strings. For example, the result of 3 + 5 will be 8, while "3" + "5" will be 35. These can be easy to confuse in the beginning.






    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน

    นิยายที่ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×