doqw89029 2015-10-18 04:49
浏览 7

学习Golang的人应该注意哪些常见的“ Go-tcha”? [关闭]

For example, the "Effective Go" documentation has the following entries:

Like C, Go's formal grammar uses semicolons to terminate statements, but unlike in C, those semicolons do not appear in the source. Instead the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them.

Cut off some parts for brevity.

Write them like this

if i < f() {
    g()
}

not like this

if i < f()  // wrong!
{           // wrong!
    g()
}

The wrong version when executed produces the following error messages:

/tmp/test.go:6: missing condition in if statement
/tmp/test.go:6: true evaluated but not used

IMO, both messages don't give the coder a clue about misplaced curly braces. Had I failed to read the documentation above, I would have probably written some Go code in the future using the wrong version (I usually use the 1st version in writing code that uses curly braces), then banged my head as to why I'm missing an "if" condition when one is clearly present.

Are there other "gotchas" in Golang that I should be aware of?

  • 写回答

1条回答 默认 最新

  • doutan3192 2015-10-18 05:08
    关注

    This could've been just a comment, but posted as an answer to lay more emphasis on it, because I do believe the linked article is really good, useful even for non-starters and it is still unknown.


    This is the best and most complete I've seen:

    50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

    It has topics ranging from total beginner (like "Opening Brace Can't Be Placed on a Separate Line" or "Unused Imports") to advanced beginner (like "nil" Interfaces and "nil" Interfaces Values or "Preemptive Scheduling").

    So a nice summary of the common pitfalls or "gotchas" is basically their table of contents, all of which you can read more about there:

    Total Beginner:

    • Opening Brace Can't Be Placed on a Separate Line
    • Unused Variables
    • Unused Imports
    • Short Variable Declarations Can Be Used Only Inside Functions
    • Redeclaring Variables Using Short Variable Declarations
    • Accidental Variable Shadowing
    • Can't Use "nil" to Initialize a Variable Without an Explicit Type
    • Using "nil" Slices and Maps
    • Map Capacity
    • Strings Can't Be "nil"
    • Array Function Arguments
    • Unexpected Values in Slice and Array "range" Clauses
    • Slices and Arrays Are One-Dimensional
    • Accessing Non-Existing Map Keys
    • Strings Are Immutable
    • Conversions Between Strings and Byte Slices
    • Strings and Index Operator
    • Strings Are Not Always UTF8 Text
    • String Length
    • Missing Comma In Multi-Line Slice/Array/Map Literals
    • log.Fatal and log.Panic Do More Than Log
    • Built-in Data Structure Operations Are Not Synchronized
    • Iteration Values For Strings in "range" Clauses
    • Iterating Through a Map Using a "for range" Clause
    • Fallthrough Behavior in "switch" Statements
    • Increments and Decrements
    • Bitwise NOT Operator
    • Operator Precedence Differences
    • Unexported Structure Fields Are Not Encoded
    • App Exits With Active Goroutines
    • Sending to an Unbuffered Channel Returns As Soon As the Target Receiver Is Ready
    • Sending to an Closed Channel Causes a Panic
    • Using "nil" Channels
    • Methods with Value Receivers Can't Change the Original Value

    Intermediate Beginner:

    • Closing HTTP Response Body
    • Closing HTTP Connections
    • Unmarshalling JSON Numbers into Interface Values
    • Comparing Structs, Arrays, Slices, and Maps
    • Recovering From a Panic
    • Updating and Referencing Item Values in Slice, Array, and Map "for range" Clauses
    • "Hidden" Data in Slices
    • Slice Data Corruption
    • "Stale" Slices
    • Type Declarations and Methods
    • Breaking Out of "for switch" and "for select" Code Blocks
    • Iteration Variables and Closures in "for" Statements
    • Deferred Function Call Argument Evaluation
    • Deferred Function Call Execution
    • Failed Type Assertions
    • Blocked Goroutines and Resource Leaks

    Advanced Beginner:

    • Using Pointer Receiver Methods On Value Instances
    • Updating Map Value Fields
    • "nil" Interfaces and "nil" Interfaces Values
    • Stack and Heap Variables
    • GOMAXPROCS, Concurrency, and Parallelism
    • Read and Write Operation Reordering
    • Preemptive Scheduling
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b