LONDON — Libya, in the words of one lawyer, “was like the Wild West” in 2008, when Goldman Sachs bankers arrived in the capital, Tripoli.

With the lifting of sanctions against Col. Muammar el-Qaddafi’s regime, the country was beginning to open to foreign companies after years of isolation. For banks like Goldman, the desert nation had a promising potential client: a newly created sovereign wealth fund backed by billions of dollars of oil wealth.

What happened next was fought out in a London courtroom this summer.

In a trial that revealed tales of prostitutes and lavish spending on hotels and meals, the Libyans accused Goldman of exerting undue influence over its employees and pushing the fund into improper investments that led to more than $1 billion in losses. Goldman argued that the Libyan fund was far more sophisticated than it had claimed and was simply suffering from “buyer’s remorse.”

On Friday, a London judge came down heavily in Goldman’s favor, sparing the Wall Street firm from paying a huge sum in damages. The ruling also provides some relief after several embarrassing overseas predicaments that have dogged Goldman and its reputation this year.